用于汇总每个范围的时间段的SQL查询

时间:2012-11-06 08:17:06

标签: sql-server tsql time

我需要创建一个查询,以便从一个范围的交叉点总结一天的小时和晚上的小时数。 例如,使用此范围:

8AM-8PM -> day
8PM-8AM -> night

以及一个或多个开始时间段:

7.30AM 10.00PM

我想得到这个值:

12 day's hours
2.5 night's hours

我不知道哪种方法可以实现这个功能。

2 个答案:

答案 0 :(得分:0)

正如我在评论中所写,这是一个脑筋急转弯。我希望你能将它与你的代码结合起来。

declare @t table(datefrom smalldatetime, dateto smalldatetime)
insert @t values('2012-01-01 10:30', '2012-01-02 09:00')
insert @t values('2012-01-01 08:00', '2012-01-02 20:20')
insert @t values('2012-01-01 19:00', '2012-01-02 13:00')
insert @t values('2012-01-01 20:00', '2012-01-02 00:00')
insert @t values('2012-01-01 23:00', '2012-01-03 00:00')

select datefrom, dateto, day, cast(datediff(minute, datefrom, dateto)/60.0 - day as decimal(9,2)) night
from @t t
cross apply
(select dateadd(day, datediff(hour, '1900-01-01 20:00', datefrom)/24, '1900-01-02 08:00') a,
dateadd(day, datediff(hour, '1901-01-01 09:00', dateto)/24, '1901-01-01 20:00' ) b) c
cross apply 
(select cast(((datediff(hour, a, b) - 12 * datediff(day, a,b)) * 60
+case when a < datefrom then -datediff(minute, a, datefrom) else 0 end
+case when b > dateto then +datediff(minute, b, dateto) else 0 end) / 60.0 as decimal(9,2)) day) e

结果:

datefrom          dateto               day    night
2012-01-01 10:30  2012-01-02 09:00:00  10.50  12.00
2012-01-01 08:00  2012-01-02 20:20:00  24.00  12.33
2012-01-01 19:00  2012-01-02 13:00:00   6.00  12.00
2012-01-01 20:00  2012-01-02 00:00:00   0.00   4.00
2012-01-01 23:00  2012-01-03 00:00:00  12.00  13.00

答案 1 :(得分:0)

很抱歉再添加一个答案。但我意识到你似乎只处理时间,我的其他解决方案在几天内运行。这是一个只处理时间的解决方案:

declare @t table(timefrom time(0), timeto time(0))
insert @t values('10:30', '21:30')
insert @t values('07:00', '20:20')
insert @t values('19:00', '21:00')
insert @t values('00:00', '23:59')
insert @t values('00:00', '00:00')

select timefrom, timeto, 
cast(dayminutes/60.0 as decimal(5,2)) [day hours]
,cast((datediff(minute, timefrom, timeto) - a.dayminutes)/60.0 as decimal(5,2)) [night hours]
from @t
cross apply
(select datediff(minute, case when timefrom > '08:00' then timefrom else '08:00' end
,case when timeto < '20:00' then case when 
timeto < '08:00' then '08:00' else timeto end else '20:00' end ) dayminutes) a

结果:

timefrom    timeto    day hours  night hours
10:30:00    21:30:00  9.50       1.50
07:00:00    20:20:00  12.00      1.33
19:00:00    21:00:00  1.00       1.00
00:00:00    23:59:00  12.00      11.98
00:00:00    00:00:00  0.00       0.00