我已经为每个时期累积了年初至今的值。但我需要每个产品组/期间的定期值。
您是否知道SQL Server中的解决方案以简单的方式实现此目的?
这是我的样本数据:
Product Group | Period | Amount
-------------------------------
Group 1 |2018/01 | 500
Group 1 |2018/02 | 740
Group 1 |2018/03 | 900
Group 1 |2018/04 | 930
结果应如下所示:
Product Group | Period | Amount
-------------------------------
Group 1 |2018/01 | 500
Group 1 |2018/02 | 240
Group 1 |2018/03 | 160
Group 1 |2018/04 | 30
谢谢你的帮助!菲利普
您可以像这样使用LAG函数:
SELECT [Product Group], Period,
Amount - LAG(Amount, 1,0) OVER (ORDER BY Period) AS Amount
FROM myTable
假设您不能使用LAG(例如MSSQL 2008)并且您可以为每个ProductGroup和Period创建多条记录,则可以使用以下查询。
样本表和值:
CREATE TABLE GR (Product_Group VARCHAR(10), Period VARCHAR(6), Amount INT);
INSERT INTO GR VALUES ('Group 1', '201801', 500)
, ('Group 1', '201802', 740)
,('Group 1', '201803', 900)
,('Group 1', '201804', 930)
;
INSERT INTO GR VALUES ('Group 2', '201801', 500)
, ('Group 2', '201803', 800)
,('Group 2', '201803', 1000)
,('Group 2', '201804', 1200)
;
查询使用GROUP BY(和CTE来简化读取)以对ProductGroup和Period进行分组,并使用RowNumber来查找以前的值(如果您有固定的时间段,即如果您希望显示每个月缺少月份值的记录,则可以使用理货日期表)
WITH X AS (SELECT Product_Group, Period, SUM(Amount) AS Amount_TOT
, ROW_NUMBER() OVER (PARTITION BY Product_Group ORDER BY PERIOD) AS RN
FROM GR GROUP BY Product_group, Period)
SELECT Product_Group, Period, Amount_TOT, Amount_TOT_PREC, Amount_TOT-ISNULL(Amount_TOT_PREC,0) AS Delta
FROM (SELECT A.Product_Group, A.Period, A.Amount_TOT, B.Amount_TOT AS Amount_TOT_PREC
FROM X A
LEFT JOIN X B ON A.Product_Group=B.Product_Group AND A.RN-1 = B.RN
) C
产量
+---------------+--------+------------+-----------------+-------+
| Product_Group | Period | Amount_TOT | Amount_TOT_PREC | Delta |
+---------------+--------+------------+-----------------+-------+
| Group 1 | 201801 | 500 | NULL | 500 |
| Group 1 | 201802 | 740 | 500 | 240 |
| Group 1 | 201803 | 900 | 740 | 160 |
| Group 1 | 201804 | 930 | 900 | 30 |
| Group 2 | 201801 | 500 | NULL | 500 |
| Group 2 | 201803 | 1800 | 500 | 1300 |
| Group 2 | 201804 | 1200 | 1800 | -600 |
+---------------+--------+------------+-----------------+-------+
使用LAG(此函数返回先前记录的值,请检查Microsoft文档)更快,更易读:
WITH X AS (SELECT Product_Group, Period
, SUM(Amount) AS Amount_TOT
FROM GR GROUP BY Product_group, Period)
SELECT Product_Group, Period, Amount_TOT, AMOUNT_PREC
, Amount_TOT-ISNULL(AMOUNT_PREC,0) AS Delta
FROM (SELECT Product_Group, Period, Amount_TOT
, LAG(Amount_TOT) OVER (PARTITION BY Product_Group
ORDER BY PERIOD) AS AMOUNT_PREC
FROM X) A;
与上面相同的输出
希望这可以帮助:
Select
ProductGroup,
Period,
Amount = Amount-ISNULL((Select Amount from TempTable where Period = (select max(Period) from TempTable where Period < Main.Period)),0)
From TempTable Main
Order by Period
我假设Period是日期时间类型(2018-01-01)
我希望它会对你有所帮助。
WITH CTE AS (
SELECT
rownum = ROW_NUMBER() OVER (ORDER BY t.[Product Group],t.[Period]),
t.*
FROM YourTbl AS t
)
SELECT CTE.[Product Group],CTE.Period,-1*(ISNULL(prev.Amount,0)-CTE.Amount) AS Amount
FROM CTE
LEFT JOIN CTE prev ON prev.rownum = CTE.rownum - 1
我会用lag()
函数:
select [Product Group], Period,
Amount - LAG(Amount, 1,0) OVER (ORDER BY Period) AS Amount
from table t'
如果你有旧版本的SQL,那么你也可以使用apply
:
select t.product, t.period, coalesce(t.amount-t1.amount, t.amount) as amount
from table t outer apply
( select top (1) t1.*
from table t1
where t1.product = t.product and
t1.period < t.period
order by t1.period desc
) t1;
查询中的一个小变化
SELECT [ProductGroup], Period,
Amount- LAG(Amount, 1,0) OVER (ORDER BY Period) AS Amount
FROM tablename