2013-12-18 87 views
2

大家好,我正试图得到2个查询的最终总和。如何求和来自2个查询的值?

我对欧元这笔款项与2013年

这里是我的查询http://sqlfiddle.com/#!2/a2638/14

JAN FEB  MAR APR  MAY  JUN  JUL AUG  SEP  OCT  NOV  DEC 
4900 4900 6400 6400 6400 6400 6400 6400 2400 2400 2400 2400 

我有这样的总和dolars以欧元,2013年

这里是我的查询http://sqlfiddle.com/#!2/a2638/15

JAN FEB  MAR APR  MAY  JUN JUL  AUG SEP  OCT NOV DEC 
15386 15386 20096 20096 20096 20096 20096 20096 7536 7536 7536 7536 

我试图让dolars的这样一个最后的总和:

JAN FEB  MAR APR  MAY  JUN JUL  AUG SEP  OCT NOV DEC 
20286 20286 26496 26496 26496 26496 26496 26496 9936 9936 9936 9936 

下面是查询解释一切http://sqlfiddle.com/#!2/a2638/12

请有人可以帮助我吗?

我会很感激帮助从SqlFiddle

+2

您将美元和欧元一起添加,你只是添加它们,或者你想在添加之前将它们转换为相同的货币吗? “有汇总”的 – naththedeveloper

+0

可能是您要查找的内容。 –

+1

嗯,你看到的链接。第一个查询是欧元的总和,第二个我乘以@euro值将欧元换算成德拉尔。我想要的是像我的帖子 –

回答

1

尝试运行此:

SELECT 
SUM(if (CONCAT(@year, '-01') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance * IF (type_money = 2, @euro, 1), 0)) Jan, 
SUM(if (CONCAT(@year, '-02') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance * IF (type_money = 2, @euro, 1), 0)) Feb, 
SUM(if (CONCAT(@year, '-03') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance * IF (type_money = 2, @euro, 1), 0)) Mar, 
SUM(if (CONCAT(@year, '-04') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance * IF (type_money = 2, @euro, 1), 0)) Apr, 
SUM(if (CONCAT(@year, '-05') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance * IF (type_money = 2, @euro, 1), 0)) May, 
SUM(if (CONCAT(@year, '-06') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance * IF (type_money = 2, @euro, 1), 0)) Jun, 
SUM(if (CONCAT(@year, '-07') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance * IF (type_money = 2, @euro, 1), 0)) Jul, 
SUM(if (CONCAT(@year, '-08') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance * IF (type_money = 2, @euro, 1), 0)) Aug, 
SUM(if (CONCAT(@year, '-09') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance * IF (type_money = 2, @euro, 1), 0)) Sep, 
SUM(if (CONCAT(@year, '-10') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance * IF (type_money = 2, @euro, 1), 0)) Oct, 
SUM(if (CONCAT(@year, '-11') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance * IF (type_money = 2, @euro, 1), 0)) Nov, 
SUM(if (CONCAT(@year, '-12') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance * IF (type_money = 2, @euro, 1), 0)) `Dec` 

FROM insurances i 
INNER JOIN policies p ON p.id = i.policy_id 
WHERE (i.initial_date >= p.date_ini 
AND i.final_date <= p.date_expired) 
; 

如果你将有更多的货币最好是introducee“货币表,并将其添加到加入。看看http://sqlfiddle.com/#!2/46cba/2

+0

@CarlitosMorales如果您以后添加日元,加元等等。 – Barmar

+0

@bmanvelyan感谢您的帮助,它的工作非常完美,并且给了我2个选项。由于我知道这个查询需要优化,因为它太大了 –

1

剪切和粘贴:

select a.Jan * @euro + b.Jan Jan, a.Feb * @euro + b.Feb Feb..... 
    from 
    (
     SELECT 
     SUM(if (CONCAT(@year, '-01') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance, 0)) Jan, 
     SUM(if (CONCAT(@year, '-02') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance, 0)) Feb, 
     .... 
     1 as Id 

     FROM insurances i 
     INNER JOIN policies p ON p.id = i.policy_id 
     WHERE (i.initial_date >= p.date_ini 
     AND i.final_date <= p.date_expired) 
     AND type_money = 1 
    ) a 
    inner join 
    (
     SELECT 
     SUM(if (CONCAT(@year, '-01') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance, 0) * @euro) Jan, 
     SUM(if (CONCAT(@year, '-02') BETWEEN date_format(date_ini, '%Y-%m') AND date_format(date_expired, '%Y-%m'),i.net_insurance, 0) * @euro) Feb, 
     .... 
    1 as Id 

    FROM insurances i 
    INNER JOIN policies p ON p.id = i.policy_id 
    WHERE (i.initial_date >= p.date_ini 
    AND i.final_date <= p.date_expired) 
    AND type_money = 1 
) b on b.Id = a.Id 

即把两个查询一起;为每个ID添加一个Id字段,以便他们可以加入,并根据需要添加/乘法。

+0

为什么你需要ID字段,如果他们都是一样的?只需做一个'CROSS JOIN'即可获得交叉产品。 – Barmar

+0

好点。不知道我为什么没有! (可能惊叹于我刚刚遇到的SqlFiddler!) –