MySQL滚动总和累积序列化

时间:2018-10-19 06:26:07

标签: mysql serialization cumulative-sum

例如,我有下面的表格(tb_transaction)

id_trans    date_trans  production_plant    dead_plant  distribution_plant
25          2017-12-31  1000                100             200
26          2018-01-17  150                 0               0
27          2018-02-07  0                   50              100
28          2018-03-07  250                 0               75
29          2018-05-10  500                 50              0

比起本年度,我尝试制作一个报表表,例如波纹管表

month   EarlyStock  production  dead    LivePlant   Distri  EndStock    
January                 150         0       150         0       150 
February                0           50      -50         100     -150    
March                   250         0       250         75      175 
April                   0           0       0           0       0   
May                     500         50      450         0       450 
June                    0           0       0           0       0   
July                    0           0       0           0       0   
August                  0           0       0           0       0   
September               0           0       0           0       0   
October                 0           0       0           0       0   
November                0           0       0           0       0   
December                0           0       0           0       0

1月的EarlyStock是2017年12月的EndStock(假设12月的EarlyStock是0),这是tb_transaction的第一个数据,而2月的EarlyStock是1月的EndStock,依此类推。

我的预期表是 比起本年度,我尝试制作一个报表表,例如波纹管表

month   EarlyStock  production  dead    LivePlant   Distri  EndStock    
January     700         150         0       850         0       850 
February    850         0           50      800         100     700 
March       700         250         0       950         75      875 
April       875         0           0       875         0       875 
May         875         500         50      1325        0       1325    
June                    0           0       0           0       0   
July                    0           0       0           0       0   
August                  0           0       0           0       0   
September               0           0       0           0       0   
October                 0           0       0           0       0   
November                0           0       0           0       0   
December                0           0       0           0       0
  

公式为:

  • LivePlant = EarlyStock +生产-已死
  • EndStock = LivePlant-Distri

任何Sugestion我该怎么做?

Here the db-fiddle for test

1 个答案:

答案 0 :(得分:0)

它看起来像Rolling Sum problem。使用 Window Functions 中的MySQL 8.0.2 and onwards,可以用不太冗长的方式来完成此操作。但是,自您的MySQL version is 5.6起,我们可以使用User-defined Session variables来模拟这种行为。

此技术的基本要点是:

  • 首先,在Derived table中,计算特定年份和月份中各种活动(如Dead,Distributed等)的总和值。就您而言,您拥有跨不同年份的数据,因此仅对“月”进行分组的方法将行不通。您需要按年份和月份分组。此外,仅将结果集限制为“当前年度”也无济于事,因为您将需要从上一年的12月开始的期末库存价值,才能获得下一年1月的“早期”库存价值。
  • 现在,使用此子选择查询的结果集,并根据给定的定义确定最终库存和早期库存。从概念上讲,这就像编写应用程序代码(例如:PHP);我们将上一行的期末库存值用作当前行的早期库存。最后,将“结束库存”值设置为当前行的结束库存(后计算)。
  • 现在,由于您不希望与上一年相对应的行;我建议您可以忽略应用程序代码中的该行。仍然,如果您只想在查询中处理它;那么您将不得不再次将完整的结果集作为“派生表”,并使用RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*) RewriteRule . ../app%1/scripts/api/index.php?fn=%2&%3 [L] 来过滤掉除当前年份以外的年份中的行。

尝试以下代码( DB Fiddle DEMO ):

Where