表格如下:
time
9:05
9:06
9:10
9:21
9:22
9:24
9:30
9:31
...
希望得到如下结果
9:05 9:10
9:21 9:24
9:30 9:31
间隔超过5分钟的,划分一组。
------解决方案--------------------
declare @tb table(t datetime)
declare @tb2 table(t1 datetime,t2 datetime)
insert @tb values( '9:05 ')
insert @tb values( '9:06 ')
insert @tb values( '9:10 ')
insert @tb values( '9:21 ')
insert @tb values( '9:22 ')
insert @tb values( '9:24 ')
insert @tb values( '9:30 ')
insert @tb values( '9:31 ')
insert into @tb2
select * from @tb a,@tb b where a.t <b.t and datediff(minute,a.t,b.t) <=5
delete a from @tb2 a,@tb2 b where a.t1> =b.t1 and a.t2 <=b.t2 and (not (a.t1=b.t1 and a.t2=b.t2))
select * from @tb2
t1 t2
----------------------- -----------------------
1900-01-01 09:05:00.000 1900-01-01 09:10:00.000
1900-01-01 09:21:00.000 1900-01-01 09:24:00.000
1900-01-01 09:30:00.000 1900-01-01 09:31:00.000
(3 行受影响)