SQL 服务器 LAG 函数的递减值不是 return 第二行之后的预期值
Decremental value with SQL Server LAG function not return the expected value after the 2nd row
我正在尝试使用 LAG 函数 return SQL SELECT 语句中的递减列。直到第二行的结果是预期的,但第三行的结果不同。
原始数据集:
AvailableId
Date
Hour
Consumed
Available
158632
2022-05-03
08
4
50
158632
2022-05-03
09
18
50
158632
2022-05-03
10
28
50
预期结果是更多一列,剩余递减(可用 - 消耗):
AvailableId
Date
Hour
Consumed
Available
Remaining
158632
2022-05-03
08
4
50
50
158632
2022-05-03
09
18
50
46
158632
2022-05-03
10
28
50
28
我试过查询:
select
AvailableId
,Date
,Hour
,Consumed
,Available
,case
when row_number() over(partition by AvailableId order by Date asc, Hour asc) = 1 then Available
when row_number() over(partition by AvailableId order by Date asc, Hour asc) = 2 then (Available - lag( Consumed ) over(partition by AvailableId order by Date asc, Hour asc))
else ((Available- lag( Consumed ) over(partition by AvailableId order by Date asc, Hour asc)) - lag( Consumed ) over(partition by AvailableId order by Date asc, Hour asc))
end as Remaining
from
#temp
但结果是:
AvailableId
Date
Hour
Consumed
Available
Remaining
158632
2022-05-03
08
4
50
50
158632
2022-05-03
09
18
50
46
158632
2022-05-03
10
28
50
14
在第二行之后递减不起作用(46 - 18),这工作正常只有 AvailableId 有两行。在这种情况下,有人知道如何 return 第 3、4 行的预期结果,直到 N 行?
DDL:
create table #temp(
[AvailableId] int not null
,[Date] date not null
,[Hour] int not null
,[Consumed] int null
,[Available] int null
)
go
insert into #temp values
(158632,'2022-03-05',08, 4, 50),
(158632,'2022-03-05',09, 18, 50),
(158632,'2022-03-05',10, 28, 50)
go
我遵循#Larnu 的提示,我可以 return 使用累积总和的预期结果:
with cte as(
select
t1.AvailableId
,t1.Date
,t1.Hour
,t1.Consumed
,t1.Available
,sum(t1.Consumed) over(partition by t1.AvailableId order by t1.Date asc, t1.Hour asc) as CumSum
from
#temp as t1
)
select
t2.AvailableId
,t2.Date
,t2.Hour
,t2.Consumed
,t2.Available
,case
when lag(t2.CumSum) over(partition by t2.AvailableId order by t2.Date asc, t2.Hour asc) is null then t2.Available
else t2.Available - lag(t2.CumSum) over(partition by t2.AvailableId order by t2.Date asc, t2.Hour asc)
end as Consumed
from
cte as t2
谢谢@Larnu!
我正在尝试使用 LAG 函数 return SQL SELECT 语句中的递减列。直到第二行的结果是预期的,但第三行的结果不同。
原始数据集:
AvailableId | Date | Hour | Consumed | Available |
---|---|---|---|---|
158632 | 2022-05-03 | 08 | 4 | 50 |
158632 | 2022-05-03 | 09 | 18 | 50 |
158632 | 2022-05-03 | 10 | 28 | 50 |
预期结果是更多一列,剩余递减(可用 - 消耗):
AvailableId | Date | Hour | Consumed | Available | Remaining |
---|---|---|---|---|---|
158632 | 2022-05-03 | 08 | 4 | 50 | 50 |
158632 | 2022-05-03 | 09 | 18 | 50 | 46 |
158632 | 2022-05-03 | 10 | 28 | 50 | 28 |
我试过查询:
select
AvailableId
,Date
,Hour
,Consumed
,Available
,case
when row_number() over(partition by AvailableId order by Date asc, Hour asc) = 1 then Available
when row_number() over(partition by AvailableId order by Date asc, Hour asc) = 2 then (Available - lag( Consumed ) over(partition by AvailableId order by Date asc, Hour asc))
else ((Available- lag( Consumed ) over(partition by AvailableId order by Date asc, Hour asc)) - lag( Consumed ) over(partition by AvailableId order by Date asc, Hour asc))
end as Remaining
from
#temp
但结果是:
AvailableId | Date | Hour | Consumed | Available | Remaining |
---|---|---|---|---|---|
158632 | 2022-05-03 | 08 | 4 | 50 | 50 |
158632 | 2022-05-03 | 09 | 18 | 50 | 46 |
158632 | 2022-05-03 | 10 | 28 | 50 | 14 |
在第二行之后递减不起作用(46 - 18),这工作正常只有 AvailableId 有两行。在这种情况下,有人知道如何 return 第 3、4 行的预期结果,直到 N 行?
DDL:
create table #temp(
[AvailableId] int not null
,[Date] date not null
,[Hour] int not null
,[Consumed] int null
,[Available] int null
)
go
insert into #temp values
(158632,'2022-03-05',08, 4, 50),
(158632,'2022-03-05',09, 18, 50),
(158632,'2022-03-05',10, 28, 50)
go
我遵循#Larnu 的提示,我可以 return 使用累积总和的预期结果:
with cte as(
select
t1.AvailableId
,t1.Date
,t1.Hour
,t1.Consumed
,t1.Available
,sum(t1.Consumed) over(partition by t1.AvailableId order by t1.Date asc, t1.Hour asc) as CumSum
from
#temp as t1
)
select
t2.AvailableId
,t2.Date
,t2.Hour
,t2.Consumed
,t2.Available
,case
when lag(t2.CumSum) over(partition by t2.AvailableId order by t2.Date asc, t2.Hour asc) is null then t2.Available
else t2.Available - lag(t2.CumSum) over(partition by t2.AvailableId order by t2.Date asc, t2.Hour asc)
end as Consumed
from
cte as t2
谢谢@Larnu!