tsql比较两个时间之间的工作时间

我在这个网址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
 
结果如下:
 

转载于:https://www.cnblogs.com/fgynew/archive/2010/07/19/1780996.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值