日期:2014-05-17 浏览次数:20406 次
if object_id('tb') is not null
drop table tb
go
create table tb(ID char(3),T1 datetime,T2 datetime)
go
insert into tb
select '001','2013-01-01 12:00:00','2013-1-1 12:00:30'
union all select '001' , '2013-01-01 12:02:23' , '2013-1-1 12:03:20'
union all select '001' , '2013-01-1 12:03:40' , '2013-1-1 12:05:30'
union all select '002', '2013-01-1 12:20:03' , '2013-1-1 12:22:23'
union all select '002' , '2013-1-1 22:00:00' , '2013-1-1 12:00:30'
union all select '002' , '2013-1-14 08:30:00' , '2013-1-14 08:31:30'
select ID,convert(char(10),T1,120) T1,count(*) icount from
( select a.ID,a.T1,min(B.T1) as T3 from tb as a left join tb as b on a.ID=b.ID
and convert(char(10),a.T1,120)=convert(char(10),b.T1,120)and a.T1<b.T1
group by a.ID,a.T1 ) as a
where T3 is null or datediff(n,T1,T3)>3
group by ID,convert(char(10),T1,120)
/*
001 2013-01-01 1
002 2013-01-01 2
002 2013-01-14 1
*/