我有一个表,其中有几列PersonID,Amount和StartDate,并提供了1个人作为示例。我想做的是 自动计算BeforeAmountCalculatedColumn,基本上是从上一行(按日期)开始 数量是。我该怎么办?
Create Table #Results
(
PersonID int,
Amount money,
StartDate datetime,
BeforeAmountCalculatedColumn money
)
insert into #Results
(
PersonID,
Amount,
StartDate,
BeforeAmountCalculatedColumn
)
select
1,
'163.45',
'30 Jan 2019',
'202.70'
union all
select
1,
'202.70',
'23 Nov 2018',
'189.45'
union all
select
1,
'189.45',
'28 Aug 2018',
'0'
答案 0 :(得分:2)
您要寻找lag()
吗?
select r.*,
lag(amount) over (partition by personid order by startdate) as BeforeAmountCalculatedColumn
from #Results r;