MySQL:A列和B列到B列的累积总和

时间:2019-04-29 06:43:42

标签: mysql sql

我有以下示例数据。 HERE 我需要获取每个日期,小时,商店的 abc-xyz 列和 reqdcolumn 列的累积总和。 我的mysql版本是5.7,Row over解决方案不适用于我的mysql版本。

样本数据

date      hour  Shop       abc     xyz  Diffabcxyz  ReqdColumn
---------------------------------------------------------------
20190428    1       1       0       0       0           0
20190428    2       1       5       4       1           0
20190428    3       1       15      8       7           1
20190428    4       1       16      9       7           8
20190428    5       1       14      13      1           15
20190428    6       1       8       12      -4          16
20190428    7       1       14      16      -2          12
20190428    8       1       9       7       2           10
20190428    9       1       5       2       3           12
20190428    10      1       6       2       4           15
20190428    1       2       0       0       0           0
20190428    2       2       7       1       6           0
20190428    3       2       5       -2      3           6
20190428    4       2       6       -5      1           9
20190428    5       2       4       7       -3          10
20190428    6       2       5       -8      -3          7
20190428    7       2       2       -9      -7          4
20190428    8       2       9       -10     -1          -3
20190428    9       2       6       4       2           -4
20190428    10      2       2       -12     -10         -2
20190428    1       3       0       0       0           0
20190428    2       3       6       -11     -5          0
20190428    3       3       8       -4      4           -5
20190428    4       3       5       -5      0           -1
20190428    5       3       9       8       1           -1
20190428    6       3       2       -1      1           0
20190428    7       3       4       4       0           1
20190428    8       3       1       2       -1          1
20190428    9       3       11      -4      7           0
20190428    10      3       0       1       -1          7

我试图做一个累加的总和,但是不知道如何对结果集进行分组。

经过测试的代码:

测试选择

set @csum := 0;

SELECT 
    date, hour, shop, (@csum:=@csum + Diffabcxyz)
FROM
    SampleTable
GROUP BY date, hour, shop;

测试更新

set @csum := 0;
update sampletable
set reqdcolumn = (@csum := @csum + Diffabcxyz);

我需要按日期,时间和商店对结果集进行分组。

样本数据和所需结果为here ReqdColumn 是所需的输出

1 个答案:

答案 0 :(得分:1)

您可以实现SQL Server LAG的功能以获取该累加和:

select @lagDate := 0, @lagShop := 0, @diffLag := 0, @cumSum := 0;

select *,
       case when @lagDate = Date and @lagShop = Shop then @cumSum := @cumSum + @diffLag else @cumSum := 0 end,
       @diffLag := Diffabcxyz,
       @lagDate := Date,
       @lagShop := Shop
from tbl
order by shop, date, hour

Demo

有关评论的更新:

  

能否请您告诉我我如何不进行前3个小时的累计计数?

select @lagDate := 0, @lagShop := 0, @diffLag := 0, @cumSum := 0, @diffLagLag := 0;

select *,
       case when @lagDate = Date and @lagShop = Shop then @cumSum := @cumSum + @diffLagLag else @cumSum := 0 end,
       @diffLagLag := @diffLag,
       @diffLag := Diffabcxyz,
       @lagDate := Date,
       @lagShop := Shop
from tbl
order by shop, date, hour

Another demo

另一个更新:

select @lagDate := 0, @lagShop := 0, @diffLag := 0, @cumSum := 0;

select *,
       case when @lagDate = Date and @lagShop = Shop and Hour > 3 then @cumSum := @cumSum + @diffLag else @cumSum := 0 end,
       @diffLag := Diffabcxyz,
       @lagDate := Date,
       @lagShop := Shop
from tbl
order by shop, date, hour

Yet another demo