[英]Mysql cumulative sum and group by month year
How can I make a cumulative sum of mysql column and group by month year here is my table "saving" 我如何才能使mysql列和按月分组的累积总和在这里是我的表“保存”
id date value
1 01/01/2018 10
2 01/12/2018 5
3 02/03/2018 6
4 02/23/2018 4
I try this query 我尝试这个查询
SET @SumVariable:=0;
select CONCAT(LEFT(monthname(date),3),'-', year(date)) as month
,sum(value)
,(@SumVariable := @SumVariable + sum(value)) AS total
FROM saving
GROUP BY year(date), month(date) LIMIT 50
The query returns 查询返回
month value
jan-2018 15
feb-2018 10
It's not cumulative the sum, I need the output to be as follow 这不是累积的总和,我需要输出如下
month total
jan-2018 15
feb-2018 25
Try this : 尝试这个 :
select date_format(s1.date,'%b-%Y') as month,
(select sum(s2.value)
from saving s2
where s2.date <= last_day(s1.date)) as total
from saving s1
group by month
order by s1.date
limit 50;
You can see more information on this question : 您可以查看有关此问题的更多信息:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.