sql server如何获取下一条记录的生效日期赋给上一条记录为结束日期
请大家帮忙..
1.原始表数据如下:
ID eid badge effectdate
----------- ----------- -------------------- -----------------------
23 72 00000001 2013-11-09 00:00:00
24 72 00000001 2013-12-01 00:00:00
25 72 00000001 2013-12-05 00:00:00
想通过SQL,变成..(就是说,下一行的effectdate就是上一列的enddate)
ID eid badge effectdate ENDDATE
----------- ----------- -------------------- --------------------------------------
23 72 00000001 2013-11-09 00:00:00 2013-12-01 00:00:00
24 72 00000001 2013-12-01 00:00:00 2013-12-05 00:00:00
25 72 00000001 2013-12-05 00:00:00 null
请各位帮忙... 谢谢.
[解决办法]
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-12-10 14:56:29
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
--Dec 28 2012 20:23:12
--Copyright (c) Microsoft Corporation
--Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([ID] int,[eid] int,[badge] varchar(8),[effectdate] datetime)
insert [huang]
select 23,72,'00000001','2013-11-09 00:00:00' union all
select 24,72,'00000001','2013-12-01 00:00:00' union all
select 25,72,'00000001','2013-12-05 00:00:00'
--------------开始查询--------------------------
select * ,(SELECT TOP 1 [effectdate] FROM huang a WHERE a.id>huang.id )[enddate]
from [huang]
----------------结果----------------------------
/*
ID eid badge effectdate enddate
----------- ----------- -------- ----------------------- -----------------------
23 72 00000001 2013-11-09 00:00:00.000 2013-12-01 00:00:00.000
24 72 00000001 2013-12-01 00:00:00.000 2013-12-05 00:00:00.000
25 72 00000001 2013-12-05 00:00:00.000 NULL
*/