累计天数 [英] Cumulative sum over days
本文介绍了累计天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的MySQL表:
I have a MySQL table like the following:
date count
2010-01-01 5
2010-01-02 6
2010-01-03 7
如何累积到下一天的每一天的总和?这样的结果是:
How can I accumulate the sum of each day to the next one? So the result is like:
date acum per day
2010-01-01 5
2010-01-02 11
2010-01-03 18
我认为我需要某种for(每个日期)...但是没有任何线索.
I think i need some kind of for(each date)... but no clue.
最后一个查询我使用了Eric的回答. (谢谢).
Just the final query i used following answer from Eric. (thanks).
从
(SELECT count(*) operations, sum(amount), date(b.timestamp) dia
FROM transactions b group by date(b.timestamp)) t1
INNER JOIN
(SELECT count(*) operations, sum(amount), date(b.timestamp) dia
FROM transactions b group by date(b.timestamp)) t2
ON t2.dia <= t1.dia GROUP BY t1.dia
推荐答案
好吧,我认为这行得通,但不确定性能如何:
Well, I think this would work, not sure how the performance would be though:
SELECT t1.date, sum(t2.count)
FROM mytable t1 INNER JOIN mytable t2 ON t2.date <= t1.date
GROUP BY t1.date
这篇关于累计天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文