日期:2014-05-18  浏览次数:20761 次

还是时间间隔的问题
表格如下:
time  
9:05
9:10
9:11
9:15
...

想得到下面的结果集合:
5
1
4
..
以及最大/小的间隔   5/1

------解决方案--------------------
create table T([time] varchar(10))
insert T select '9:05 '
union all select '9:10 '
union all select '9:11 '
union all select '9:15 '
go


select
begTime=A.[time],
endTime=min(B.[time]),
间隔=datediff(minute, convert(datetime, A.[time], 108), convert(datetime, min(B.[time]), 108))
from T as A, T as B
where convert(datetime, A.[time], 108) <convert(datetime, B.[time], 108)
group by A.[time]

--result
begTime endTime 间隔
---------- ---------- -----------
9:05 9:10 5
9:10 9:11 1
9:11 9:15 4