【发布时间】:2015-05-05 14:26:22
【问题描述】:
我有一张下表 -
+------------------------------------------+
¦ 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
请帮助达到预期的结果。
【问题讨论】:
标签: mysql cumulative-sum