尝试从我动态求和的列中获得累计总和

Try to get a cumulative sum out of columns which I sum on the fly

我有一个关注 table -

+------------------------------------------+
¦ date       ¦ earn_points ¦ redeem_points ¦
¦------------+-------------+---------------¦
¦ 2015-05-05 ¦ 50          ¦ 30            ¦
¦------------+-------------+---------------¦
¦ 2015-05-05 ¦ 60          ¦ 30            ¦
¦------------+-------------+---------------¦
¦ 2015-05-04 ¦ 70          ¦ 50            ¦
¦------------+-------------+---------------¦
¦ 2015-05-04 ¦ 80          ¦ 40            ¦
¦------------+-------------+---------------¦
¦ 2015-05-03 ¦ 30          ¦ 20            ¦
+------------------------------------------+

我正在寻找以下结果 -

+-------------------------------------------------------------------------------------------------------------+
¦ date       ¦ total_earn_points ¦ total_redeem_points ¦ total_liability_points ¦ Cumulative_liability_points ¦
¦------------+-------------------+---------------------+------------------------+-----------------------------¦
¦ 2015-05-05 ¦ 110               ¦ 60                  ¦ 50                     ¦ 120                         ¦
¦------------+-------------------+---------------------+------------------------+-----------------------------¦
¦ 2015-05-04 ¦ 150               ¦ 90                  ¦ 60                     ¦ 70                          ¦
¦------------+-------------------+---------------------+------------------------+-----------------------------¦
¦ 2015-05-03 ¦ 30                ¦ 20                  ¦ 10                     ¦ 10                          ¦
+-------------------------------------------------------------------------------------------------------------+

我正在尝试这个 SQL 查询,但无法获得正确的累计总数:

SELECT `transaction_date`, 
IFNULL(SUM(rewards_point_rewarded),0) AS `total_earn_points`, 
IFNULL(SUM(rewards_point_redemed),0) AS `total_redeem_points`, 
(SUM(rewards_point_rewarded) - SUM(rewards_point_redemed)) AS `total_liability_points`, 
@total := @total + (SUM(rewards_point_rewarded) - SUM(rewards_point_redemed)) AS `Cumulative_liability_points` 
FROM `i_report_total_order`, (SELECT @total:=0) AS t 
WHERE (website_id = '36') 
GROUP BY `transaction_date` 
ORDER BY `transaction_date` DESC

请帮助实现预期的结果。

忽略 Alexandre 的评论,你显然已经成功了,构建了一个可靠的问题,人们总是可以选择不回答。

在MySQL.

中跨组使用@变量进行累积实际上有点雷区

我会采用先获取非累积值然后使用@变量找到解决方案的方法:

  SELECT date, 
         SUM(earn_points) AS tot_earn_pts, 
         SUM(redeem_points) AS tot_redeem_pts
    FROM i_report_total_order
/* WHERE website_id = 36 */
GROUP BY date 
ORDER BY date DESC

然后:

  SELECT date,
         tot_earn_pts,
         tot_redeem_pts,
         tot_earn_pts - tot_redeem_pts AS tot_liability_pts,
         @cum := @cum + tot_earn_pts - tot_redeem_pts AS cum_liability_pts
    FROM (   
      SELECT date, 
             SUM(earn_points) AS tot_earn_pts, 
             SUM(redeem_points) AS tot_redeem_pts
        FROM i_report_total_order
     /* WHERE website_id = 36 */
    GROUP BY date 
         ) tots
    JOIN (SELECT @cum := 0) init
ORDER BY date      

不幸的是,您必须再次包装才能获得您指定的日期顺序。但是您可以通过将列设置为 NOT NULL

来避免 IFNULLS

SQLFiddle Example

更新

SELECT *
  FROM (
    SELECT date,
           tot_earn_pts,
           tot_redeem_pts,
           tot_earn_pts - tot_redeem_pts AS tot_liability_pts,
           @cum := @cum + tot_earn_pts - tot_redeem_pts AS cum_liability_pts
      FROM (   
        SELECT date, 
               SUM(earn_points) AS tot_earn_pts, 
               SUM(redeem_points) AS tot_redeem_pts
          FROM i_report_total_order
       /* WHERE website_id = 36 */
      GROUP BY date 
           ) tots
      JOIN (SELECT @cum := 0) init
  ORDER BY date  
       ) res_asc
ORDER BY date DESC

已更新SQLFiddle

根据您的帮助,这是对我而言有效的查询

SELECT `transaction_date`, 
       `total_earn_points`, 
       `total_redeem_points`, 
       `total_liability_points`, 
       @total := @total + `total_liability_points` AS `Cumulative_liability_points` 
       FROM (
              SELECT `transaction_date`, 
              SUM(rewards_point_rewarded) AS `total_earn_points`, 
              SUM(rewards_point_redemed) AS `total_redeem_points`,
              (SUM(rewards_point_rewarded) - SUM(rewards_point_redemed)) AS `total_liability_points`  
              FROM `i_report_total_order` 
              WHERE (website_id = '36') 
              GROUP BY `transaction_date` 
              ORDER BY `transaction_date` ASC
       ) tots 
join (SELECT @total:=0) AS init 
ORDER BY `transaction_date` ASC

只有当我在最后一行设置 ASC 时它才有效。有没有办法在顶部显示最新日期。