例如,我有下面的表格(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
公式为:
任何Sugestion我该怎么做?
答案 0 :(得分:0)
它看起来像Rolling Sum problem。使用 Window Functions 中的MySQL 8.0.2 and onwards,可以用不太冗长的方式来完成此操作。但是,自您的MySQL version is 5.6起,我们可以使用User-defined Session variables来模拟这种行为。
此技术的基本要点是:
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)
RewriteRule . ../app%1/scripts/api/index.php?fn=%2&%3 [L]
来过滤掉除当前年份以外的年份中的行。尝试以下代码( DB Fiddle DEMO ):
Where