mysql 按月分组累计统计数据,纯sql实现的一个方法
在网上查阅了很多方法,我尝试了用后台java,但实现起来比较麻烦,最后自己探索用纯sql来实现,记录起来分享下。
mysql 按月分组累计统计数据,想得到如下的结果,如下图:
【year_and_month】- 显示按月分组查询的字段
【count_per_month】- 显示每月分组统计的数量
【total_by_mont】- 显示按月分组累计总数 = 本月分组统计数量 + 本月前所有月份分组统计数量
我的解决思路:
1、首先要准备或得到一个如下图结果的表 "test_table" :
2、执行一下sql
SELECT
year_and_moth,
count_per_month,
(
SELECT
SUM(count_per_month)
FROM
test_table AS test_table_1
WHERE
test_table_1.year_and_moth <= test_table_2.year_and_moth
) AS total_by_month
FROM
test_table AS test_table_2
3、得出如下结果:
4、The end.
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 用PHP实现弹出消息提示框
- 下一篇: C++ 函数返回二维数组