34,594
社区成员
发帖
与我相关
我的任务
分享
create table aa(id int,carkey varchar(15),date datetime)
insert into aa select 1,'15022104271','2007-12-11 14:59:00'
union all select 2,'15022104271','2007-12-11 15:00:00'
union all select 3,'15022104271','2007-12-11 15:03:00'
union all select 23,'15022104275','2007-12-11 15:53:00'
union all select 25,'15022104275','2007-12-11 16:05:00'
union all select 29,'15022104275','2007-12-11 16:11:00'
------------
select id,carkey,isnull(substring(convert(varchar(20),date-(select min(date)from aa where id<a.id and a.carkey=carkey),112),11,18) ,date) from aa a
if object_id('tempdb..#tb') is not null drop table #tb
go
create table #tb (ID int ,CarKey varchar(20) ,Date datetime)
go
insert into #tb select 1, '15022104271','2007-12-11 14:59:00'
union all select 2, '15022104271','2007-12-11 15:00:00'
union all select 3, '15022104271','2007-12-11 15:03:00'
union all select 23, '15022104275','2007-12-11 15:53:00'
union all select 25, '15022104275', '2007-12-11 16:05:00'
union all select 29, '15022104275','2007-12-11 16:11:00'
go
select ID,CarKey,replace(convert(varchar(20),Date-(select isnull(max(Date),0) from #tb where t.CarKey=CarKey and ID<t.ID),120),'1900-01-01 ','') as Date
from #tb t
/*
ID CarKey Date
1 15022104271 2007-12-11 14:59:00
2 15022104271 00:01:00
3 15022104271 00:03:00
23 15022104275 2007-12-11 15:53:00
25 15022104275 00:12:00
29 15022104275 00:06:00
*/