得到两行之间的差异并填充差值

时间:2016-07-27 18:43:31

标签: sql-server tsql

enter image description here我有一张下面结构的表格。 [![在此处输入图像说明] [2]] [2]
我想再计算一个具有“差异”的列名。如果guid值相同,则应计算差异,并填充两行的差异。

在差异列之后,我的表应如下所示 enter image description here

了解更多细节

我的表格如下所示

GUID                                    Date      Quantity
0001639C-8047-45FD-8FB0-D24B906D25D0    7/21/2016   30
0001639C-8047-45FD-8FB0-D24B906D25D0    7/15/2016   20
00088951-A2F6-4405-9195-4E830912D56D    7/22/2016   40
00088951-A2F6-4405-9195-4E830912D56D    7/12/2016   20
00060D8A-F711-42BD-824F-6F9F92A02E6E    7/23/2016   2
00074492-6068-48A6-8F99-F70D7328B166    7/19/2016   15
0007E203-4BD9-4937-BFCB-6A3EBCA33448    7/15/2016   2

计算差异列后,它应如下所示

GUID               Date                                 Quantity    Difference
0001639C-8047-45FD-8FB0-D24B906D25D0    7/21/2016        30                10
0001639C-8047-45FD-8FB0-D24B906D25D0    7/15/2016        20                10
00088951-A2F6-4405-9195-4E830912D56D    7/22/2016        40                20
00088951-A2F6-4405-9195-4E830912D56D    7/12/2016        20                20
00060D8A-F711-42BD-824F-6F9F92A02E6E    7/23/2016         2                 0
00074492-6068-48A6-8F99-F70D7328B166    7/19/2016        15                 0
0007E203-4BD9-4937-BFCB-6A3EBCA33448    7/15/2016     2                     0

当guid值相同时,应该计算差异列,应该执行数量的差异,并且应该在差异列中填充。如果guid值不相同,则应该填充零。< / p>

2 个答案:

答案 0 :(得分:0)

DECLARE @Table Table (GUID varchar(50), Date Date, Quantity Decimal(9,2))
Insert Into @Table (GUID,Date,Quantity) Values
('0001639C-8047-45FD-8FB0-D24B906D25D0','2016-07-21',30),
('0001639C-8047-45FD-8FB0-D24B906D25D0','2016-07-15',20),
('00088951-A2F6-4405-9195-4E830912D56D','2016-07-22',40),
('00088951-A2F6-4405-9195-4E830912D56D','2016-07-12',20),
('00060D8A-F711-42BD-824F-6F9F92A02E6E','2016-07-23',2),
('00074492-6068-48A6-8F99-F70D7328B166','2016-07-19',15),
('0007E203-4BD9-4937-BFCB-6A3EBCA33448','2016-07-15',2)

;with cteBase as (
  Select *
        ,Difference=Quantity - Lag(Quantity) over (Partition By GUID Order By Date)
   From  @Table
)
Select GUID,Date,Quantity
      ,Difference = sum(isnull(Difference,0)) over (Partition By GUID Order By Date Desc)
 From cteBase
 Order by GUID,Date Desc

返回

GUID                                    Date        Quantity    Difference
0001639C-8047-45FD-8FB0-D24B906D25D0    2016-07-21  30.00       10.00
0001639C-8047-45FD-8FB0-D24B906D25D0    2016-07-15  20.00       10.00
00060D8A-F711-42BD-824F-6F9F92A02E6E    2016-07-23  2.00        0.00
00074492-6068-48A6-8F99-F70D7328B166    2016-07-19  15.00       0.00
0007E203-4BD9-4937-BFCB-6A3EBCA33448    2016-07-15  2.00        0.00
00088951-A2F6-4405-9195-4E830912D56D    2016-07-22  40.00       20.00
00088951-A2F6-4405-9195-4E830912D56D    2016-07-12  20.00       20.00

答案 1 :(得分:0)

DECLARE @Table Table (GUID varchar(50), Date Date, Quantity Decimal(9,2))
Insert Into @Table (GUID,Date,Quantity) Values
('0001639C-8047-45FD-8FB0-D24B906D25D0','2016-07-21',30),
('0001639C-8047-45FD-8FB0-D24B906D25D0','2016-07-15',20),
('00088951-A2F6-4405-9195-4E830912D56D','2016-07-22',40),
('00088951-A2F6-4405-9195-4E830912D56D','2016-07-12',20),
('00060D8A-F711-42BD-824F-6F9F92A02E6E','2016-07-23',2),
('00074492-6068-48A6-8F99-F70D7328B166','2016-07-19',15),
('0007E203-4BD9-4937-BFCB-6A3EBCA33448','2016-07-15',2)

;with cteBase as (
  Select *
        ,Difference=Quantity - Lag(Quantity) over (Partition By GUID Order By Date)
        ,PctChange =(100*(Lag(Quantity) over (Partition By GUID Order By Date)))/Quantity
   From  @Table
)
Select GUID,Date,Quantity
      ,Difference = sum(isnull(Difference,0)) over (Partition By GUID Order By Date Desc)
      ,PctChange  = sum(isnull(PctChange ,0)) over (Partition By GUID Order By Date Desc)
 From cteBase
 Order by GUID,Date Desc

返回

GUID                                    Date        Quantity  Difference    PctChange
0001639C-8047-45FD-8FB0-D24B906D25D0    2016-07-21  30.00     10.00         66.666666666666
0001639C-8047-45FD-8FB0-D24B906D25D0    2016-07-15  20.00     10.00         66.666666666666
00060D8A-F711-42BD-824F-6F9F92A02E6E    2016-07-23  2.00      0.00          0.000000000000
00074492-6068-48A6-8F99-F70D7328B166    2016-07-19  15.00     0.00          0.000000000000
0007E203-4BD9-4937-BFCB-6A3EBCA33448    2016-07-15  2.00      0.00          0.000000000000
00088951-A2F6-4405-9195-4E830912D56D    2016-07-22  40.00     20.00         50.000000000000
00088951-A2F6-4405-9195-4E830912D56D    2016-07-12  20.00     20.00         50.000000000000