我在这个网址http://beyondrelational.com/blogs/tc/archive/2009/02/27/tsql-challenge-2.aspx发现了这个tsql 的challenge。要求计算两个给点时间之间的工作时间,假定工作日的时间是从早上8点到到下午5点。
很显然,周末的时间不能计算在工作时间内。
题目给定了假设数据时:
ID StartDate EndDate ----------- ----------------------- ----------------------- 1 2009-03-02 08:00:00.000 2009-03-02 15:00:00.000 2 2009-03-01 16:00:00.000 2009-03-03 14:00:00.000 3 2009-02-26 07:00:00.000 2009-02-26 22:00:00.000 4 2009-01-27 09:15:00.000 2009-01-27 17:15:00.000 5 2009-01-17 13:45:00.000 2009-01-19 07:45:00.000 6 2009-01-27 21:15:00.000 2009-01-28 09:15:00.000
应该得到的结果是:
StartDate EndDate Hours Minutes ----------------------- ----------------------- ----------- ----------- 2009-03-02 08:00:00.000 2009-03-02 15:00:00.000 7 0 2009-03-01 16:00:00.000 2009-03-03 14:00:00.000 15 0 2009-02-26 07:00:00.000 2009-02-26 22:00:00.000 9 0 2009-01-27 09:15:00.000 2009-01-27 17:15:00.000 7 45 2009-01-17 13:45:00.000 2009-01-19 07:45:00.000 0 0 2009-01-27 21:15:00.000 2009-01-28 09:15:00.000 1 15
下面是我的解决方案:
set datefirst 1;
with cte1(id, startDate, endDate) as(
select 1, cast('2009-03-02 08:00:00.000' as datetime), cast('2009-03-02 15:00:00.000' as datetime) union all
select 2, CAST('2009-03-01 16:00:00.000' as datetime), CAST('2009-03-03 14:00:00.000' as datetime) union all
select 3, CAST('2009-02-26 07:00:00.000' as datetime), CAST('2009-02-26 22:00:00.000' as datetime) union all
select 4, CAST('2009-01-27 09:15:00.000' as datetime), CAST('2009-01-27 17:15:00.000' as datetime) union all
select 5, CAST('2009-01-17 13:45:00.000' as datetime), CAST('2009-01-19 07:45:00.000' as datetime) union all
select 6, CAST('2009-01-27 21:15:00.000' as datetime), CAST('2009-01-28 09:15:00.000' as datetime)
)
--soluton 3
select id, startDate, endDate, SUM(case when minutes> 0 then minutes else 0 end) / 60 as hours,
SUM(case when minutes> 0 then minutes else 0 end) % 60 as minutes
from
(
select *, isWeekday * DATEDIFF(minute, propStartDate, propEndDate) as minutes
from (
select *,
case DATEPART(weekday, inStartDate)
when 6 then 0
when 7 then 0
else 1 end as isWeekday,
case
when N=1 and startDate < inStartDate then inStartDate
when N=1 and startDate > inStartDate then startDate
else inStartDate end as propStartDate,
case
when DATEDIFF(day, inEndDate, endDate) = 0 and endDate < inEndDate then endDate
when DATEDIFF(day, startDate, endDate) = 0 and endDate >= inEndDate then inEndDate
else inEndDate end as propEndDate
from (
select *, dateadd(day, N-1, DATEADD(hour,8, DATEADD(day,DATEDIFF(day, 0, startDate), 0))) as inStartDate,
dateadd(day, N-1, DATEADD(hour, 17, DATEADD(day,DATEDIFF(day, 0, startDate), 0))) as inEndDate
from cte1 c1 cross apply(
select *
from dbo.Number
where N <= DATEDIFF(day, c1.startDate, c1.endDate) + 1) as c
) as d
) as d2
) as d3
group by id, startDate, endDate
结果如下: