我有下表:
+-------------+-------+-------+
| date | agent | hold |
+-------------+-------+-------+
| 2017-01-01 | A | 100 |
| 2017-01-01 | B | 200 |
| 2017-01-02 | C | 400 |
+-------------+-------+-------+
我要添加运行总数的查询:
SET @runtot:=0;
SELECT
date,
SUM(CASE WHEN agent = 'A' THEN hold ELSE 0 END) AS A,
SUM(CASE WHEN agent = 'B' THEN hold ELSE 0 END) AS B,
SUM(CASE WHEN agent = 'C' THEN hold ELSE 0 END) AS C,
SUM(CASE WHEN agent = 'D' THEN hold ELSE 0 END) AS D,
sum(hold) as Total,
(@runtot := @runtot + sum(hold)) AS rt
FROM daily_results
group by date
但是我在rt列中得到每行的总和(hold)。我做错什么了?
1条答案
按热度按时间7xzttuei1#
按照建议使用汇总,或者将agregates移动到子查询,并在主查询中执行运行总计。举个例子