34,590
社区成员
发帖
与我相关
我的任务
分享
create table #t
(id int, a decimal(8,2), crDate datetime )
insert into #t
select 1, 112.5, '2013-05-01 08:10:01' union all
select 2, 112.5, '2013-05-01 08:10:11' union all
select 5, 100.6, '2013-05-01 08:15:01' union all
select 8, 100.6, '2013-05-01 08:20:01' union all
select 13, 100.6, '2013-05-01 08:21:01' union all
select 26, 112.5, '2013-05-01 08:22:01' union all
select 35, 112.5, '2013-05-01 08:25:01' union all
select 40, 90.5, '2013-05-01 08:40:01' union all
select 41, 90.5, '2013-05-01 08:42:01' union all
select 52, 90.5, '2013-05-01 08:42:41' union all
select 55, 100.6, '2013-05-01 08:50:01' union all
select 70, 100.6, '2013-05-01 08:51:01' union all
select 71, 100.6, '2013-05-01 08:53:01' union all
select 72, 100.6, '2013-05-01 08:55:01' union all
select 77, 90.5, '2013-05-01 09:03:01' union all
select 78, 90.5, '2013-05-01 09:04:01'
DECLARE @T TABLE(iid INT IDENTITY(1,1),id INT ,a INT,crdate DATETIME)
INSERT INTO @t(id,a,crdate)
SELECT id,a,crdate FROM dbo.TB
SELECT id ,
a ,
crdate
FROM @T
WHERE iid IN ( SELECT ( SELECT MAX(iid)
FROM @T AS B
WHERE B.iid <= A.iid
AND b.a = a.a
AND NOT EXISTS ( SELECT *
FROM @T AS C
WHERE B.iid - 1 = C.iid
AND B.a = C.a )
) AS grp
FROM @T AS A )
/*
id a crdate
1 112 2013-05-01 08:10:01.000
5 100 2013-05-01 08:15:01.000
26 112 2013-05-01 08:22:01.000
40 90 2013-05-01 08:40:01.000
55 100 2013-05-01 08:50:01.000
77 90 2013-05-01 09:03:01.000*/