日期:2014-05-17 浏览次数:20478 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (ID int,NO nvarchar(4),TIME datetime)
insert into [TB]
select 3,'01','2010-03-12 09:18:15.000' union all
select 4,'01','2010-03-12 09:19:17.077' union all
select 5,'02','2010-03-12 09:19:33.857' union all
select 6,'01','2010-03-12 09:30:43.733' union all
select 7,'02','2010-03-12 13:10:12.123' union all
select 8,'04','2010-03-17 13:46:25.263' union all
select 9,'05','2010-03-17 13:48:44.967' union all
select 10,'04','2010-03-17 13:48:47.107' union all
select 11,'01','2010-03-17 13:48:48.937'
WITH TT
AS(
select *,ROW_NUMBER()OVER(PARTITION BY NO ORDER BY id) AS num from [TB])
SELECT A.id,A.no,A.TIME
FROM TT A
INNER JOIN TT B ON A.NO = B.NO AND A.num = b.num+1
WHERE DATEDIFF(mi,b.TIME,A.TIME) >15
ORDER BY A.NO,A.TIME
/*
id no TIME
11 01 2010-03-17 13:48:48.937
7 02 2010-03-12 13:10:12.123*/