我有一张桌子主页:
(
time date,
qty int
)
我想创建一个查询,所以每天我得到当天和之前所有日期的数量总和 所以这个数据
-----------------------
time | qty
01/09/2009 | 3
02/09/2009 | 8
03/09/2009 | 2
04/09/2009 | 5
我得到:
-----------------------
time | total
01/09/2009 | 3
02/09/2009 | 11
03/09/2009 | 13
04/09/2009 | 18
提前致谢
答案 0 :(得分:0)
SELECT TIME, (SELECT SUM(QTY) FROM main m2 WHERE m2.ITME <= mt1.TIME) AS sum
FROM main m1
ORDER BY TIME
这应该可以解决问题,尽管它可能不是最快的解决方案。
答案 1 :(得分:0)
SELECT
M.Time,
SUM(M2.Qty) RunningTotal
FROM
Main M
LEFT JOIN Main M2 ON M.Time >= M2.Time
GROUP BY
M.Time;
答案 2 :(得分:0)
这应该会给你一个更快的结果
SELECT time
, @tot_qty := @tot_qty+qty AS tot_qty
FROM Table1
JOIN (SELECT @tot_qty := 0) d
order by time
<强> SQL FIDDLE 强>