mysql - MySQL 中的累计和

标签 mysql sql

使用 MySQL。我想得到累计和。

这是我的 table

CREATE TABLE `user_infos`
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
(..)
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`) )

而我想要得到的是

+-------+-------+----------------+
| month | count | cumulative_sum |
+-------+-------+----------------+
|   01  |  100  | 100            |
|   02  |  101  | 201            |
|   ... |  110  | 311            |
|   12  |  200  | 511            |
+-------+-------+----------------+

但是结果是

+-------+-------+----------------+
| month | count | cumulative_sum |
+-------+-------+----------------+
|   01  |  100  | 100            |
|   02  |  101  | 101            |
|   ... |  110  | 110            |
|   12  |  200  | 200            |
+-------+-------+----------------+

这是我的错误查询..

select 
     T1.Month,T1.Count,
     @runnung_total := (@running_total + T1.Count) as cumulative_sum
from (
     select date_format(created_at,'%m') as Month,count(1) as Count from users 
          where date_format(created_at,'%Y')='2016'
          group by(date_format(created_at,'%m'))
     union
     select date_format(created_at,'%m') as Month,count(1) as Count from users 
          where date_format(created_at,'%Y')='2017'
          group by(date_format(created_at,'%m')) ) as T1
join (select @running_total := 0) as R1;

我提到了 this .我的代码有什么问题?

最佳答案

您可以分两步实现:首先获取每年和每月的总和

select  concat(year(created_at), lpad(month(created_at), 2, '0')) as ye_mo,
        count(*) as cnt
from    users
group by concat(year(created_at), lpad(month(created_at), 2, '0'))

然后将它与自身连接起来,使每一行都与之前的所有行相匹配

select  t1.ye_mo, sum(t2.cnt)
from    (
            select  concat(year(created_at), lpad(month(created_at), 2, '0')) as ye_mo,
                    count(*) as cnt
            from    users
            group by concat(year(created_at), lpad(month(created_at), 2, '0'))
        ) t1
join    (
            select  concat(year(created_at), lpad(month(created_at), 2, '0')) as ye_mo,
                    count(*) as cnt
            from    users
            group by concat(year(created_at), lpad(month(created_at), 2, '0'))
        ) t2
on      t1.ye_mo >= t2.ye_mo
group by t1.ye_mo
order by t1.ye_mo

编辑

上面的查询假设您希望运行总和在不同年份增加。如果只想显示月份,聚契约(Contract)一月不同年份的值,可以这样改id

select  t1.mnt, sum(t2.cnt)
from    (
            select  month(created_at) as mnt,
                    count(*) as cnt
            from    userss
            group by month(created_at)
        ) t1
join    (
            select  month(created_at) as mnt,
                    count(*) as cnt
            from    userss
            group by month(created_at)
        ) t2
on      t1.mnt >= t2.mnt
group by t1.mnt
order by t1.mnt

最后,如果你想在每年年初重置运行总和,你可以这样做

select  t1.yr, t1.mn, sum(t2.cnt)
from    (
            select  year(created_at) as yr, month(created_at) as mn,
                    count(*) as cnt
            from    userss
            group by year(created_at), month(created_at)
        ) t1
join    (
            select  year(created_at) as yr, month(created_at) as mn,
                    count(*) as cnt
            from    userss
            group by year(created_at), month(created_at)
        ) t2
on      t1.yr = t2.yr and
        t1.mn >= t2.mn
group by t1.yr, t1.mn
order by t1.yr, t1.mn

可以看到所有三个版本的实际效果 here

关于mysql - MySQL 中的累计和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44021237/

相关文章:

sql 'like'对mongodb中数字的操作

php - 使用 Dreamweaver 将多个检查值插入到一个数据库字段中

mysql - SSIS-MySQL 错误 "Failed to convert parameter value from a DateTime to a Decimal."

PHP MySQL 连接持久化

sql - 删除SQL Server 2008中所有非系统对象的脚本

mysql - 是否可以从 MySQL 中的查询结果创建计算行?

sql - t-sql 返回错误代码与 RaiseError

mysql - 从数据库sql文件恢复单个mysql表

mysql - 无法使用sequelize连接带有外键的2个表

MySQL 查询运行缓慢。帮忙优化一下