0

我需要创建一个查询来从一个范围相交中总结白天的小时数和夜晚的小时数。例如这个范围:

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

以及一个或多个起止时间段:

7.30AM 10.00PM

我想得到这个值:

12 day's hours
2.5 night's hours

我不知道这是完成此功能的最糟糕的方法。

4

2 回答 2

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
于 2012-11-06T13:45:23.100 回答
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
于 2012-11-06T11:25:17.100 回答