如何对2个查询中的值求和?

时间:2013-12-18 17:00:42

标签: mysql sql

大家好,我正在努力获得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

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

我正试图得到这样的最终总和:

 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

的查询

请有人帮助我吗?

我真的很感激帮助

2 个答案:

答案 0 :(得分:1)

从SqlFiddle剪切和粘贴:

    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字段,以便它们可以连接,并根据需要添加/乘法。

答案 1 :(得分: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) 
;

如果你有更多的货币,最好引入'货币表并添加它来加入。看看http://sqlfiddle.com/#!2/46cba/2