Fetch cumulative sum from MySQL table

不打扰是莪最后的温柔 提交于 2019-12-04 19:58:09

A subquery without variables will do it just as easily, and quite a bit more portably;

SELECT YEAR(`time`), 
       MONTH(`time`), 
       SUM(gross),
       (SELECT SUM(gross) 
        FROM donations 
        WHERE `time`<=MAX(a.`time`)) cumulative_gross
FROM donations a GROUP BY YEAR(`time`), MONTH(`time`);

An SQLfiddle to test with.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!