【发布时间】:2019-04-09 07:47:28
【问题描述】:
我需要为具有相同 ID 值的每一行生成一个结束日期,作为下一条记录的 date-1,如果该 ID 没有下一行,则结束日期应该为空。
例如,对于 ID 513,第一行是 2008-01-01 和 2010-04-16,第二行是 2010-04-17、2011-04-25。
我不确定如何在没有光标的情况下实现这一目标。
Create Table #Temp
(
ID int,
Amount money,
StartDate datetime
)
insert into #Temp
(
ID,
Amount,
StartDate
)
select 513,240.00,'2008-01-01 00:00:00' union all
select 513,240.00,'2010-04-17 00:00:00' union all
select 513,265.00,'2011-04-26 00:00:00' union all
select 513,275.00,'2012-04-17 00:00:00' union all
select 513,285.00,'2013-04-22 00:00:00' union all
select 513,325.00,'2015-06-15 00:00:00' union all
select 513,335.00,'2017-06-15 00:00:00' union all
select 514,280.00,'2001-01-22 00:00:00' union all
select 514,280.00,'2010-06-09 00:00:00' union all
select 515,240.00,'2019-01-01 00:00:00' union all
select 515,240.00,'2010-04-17 00:00:00' union all
select 515,265.00,'2011-04-26 00:00:00' union all
select 515,275.00,'2012-04-17 00:00:00' union all
select 515,285.00,'2013-04-22 00:00:00' union all
select 515,325.00,'2015-06-15 00:00:00' union all
select 515,335.00,'2017-06-15 00:00:00'
select * from #Temp
drop table #Temp
【问题讨论】:
标签: sql sql-server tsql