一些比较难的sql问题:2
最近,在
论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。
所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。
1、时间间隔计算.
这个问题非常复杂。
start_time end_time
2013-09-11 17:26:02.382 2013-09-24 10:38:01.41
2013-09-18 17:02:40.444 2013-09-22 15:27:58.984
2013-09-18 08:21:32.036 2013-09-22 15:31:52.499
2013-09-13 16:28:29.832 2013-09-16 09:41:47.988
2013-09-09 10:59:59.835 2013-09-10 14:06:21.223
要求计算这两个列的时间差 但是要去除9月份的正常休假并且只计算正常工作时间(上午8:30--12:00 下午14:00--18:00)
计算结果如下:
start_time end_time diff_time(小时)
2013-09-11 17:26:02.382 2013-09-24 10:38:01.41 55.1
2013-09-18 17:02:40.444 2013-09-22 15:27:58.984 5.9
2013-09-18 08:21:32.036 2013-09-22 15:31:52.499 12.5
2013-09-13 16:28:29.832 2013-09-16 09:41:47.988 2.7
2013-09-09 10:59:59.835 2013-09-10 14:06:21.223 1.1
请各位大大帮忙看看这个时间差应该怎么计算 谢谢了
我的解法:
[sql]
if object_id('tab') is not null
drop table tab
if object_id('holiday') is not null
drop table holiday
go
create table tab(start_time datetime,end_time datetime)
insert into tab
select '2013-09-11 17:26:02.382','2013-09-24 10:38:01.41' union
select '2013-09-18 17:02:40.444','2013-09-22 15:27:58.984' union
select '2013-09-18 08:21:32.036','2013-09-22 15:31:52.499' union
select '2013-09-13 16:28:29.832','2013-09-16 09:41:47.988' union
select '2013-09-09 10:59:59.835','2013-09-09 14:06:21.223'
create table holiday(h_date datetime)
insert into holiday
select '2013-09-01' union
select '2013-09-07' union
select '2013-09-08' union
select '2013-09-14'union
select '2013-09-15'union
select '2013-09-19'union
select '2013-09-20'union
select '2013-09-21'union
select '2013-09-29'
go
WITH calendar --产生日历
AS
(
SELECT CAST('2013-09-01' as varchar(10)) AS r --月份的开始日期
UNION ALL
SELECT convert(VARCHAR(10),dateadd(day,1,r),120)
FROM calendar
WHERE r < '2013-09-30' --月份的结束日期
),
tt --计算时间间隔,单位为秒
as
(
SELECT t.start_time,
t.end_time,
c.r,
h.h_date,
/*
通过tab表和calendar表的关联,就能把开始时间到结束时间,多对应的多天,
都给关联出来,
比如开始时间 2013-09-18 08:21:32.037 结束时间 2013-09-22 15:31:52.500,
其实就是,18、19、20、21、22这一共5天,会由原来的1条记录,现在变为5条记录。
如果h_date为null,说明这一天不是假日,
就需要计算时间间隔,有几种可能性:
1.开始时间和结束时间,在同一天的
2.当前日期和开始日期相同
3.当前日期和结束日期相同
4.当前日期是在开始日期和结束日期之间的某天
如果h_date是null,那么返回0,说明是节假日,就不用计算时间间隔了
*/
case when h_date IS null and
convert(varchar(10),t.start_time_temp,120) = c.r and
CONVERT(varchar(10),t.end_time_temp,120) = c.r
then case when convert(varchar(5),t.start_time_temp,114) between '08:30' and '12:00'
and not (convert(varchar(5),t.end_time_temp,114) between '08:30' and '12:00')
then DATEDIFF(second,t.start_time_temp,c.r +' 12:00:00')
else 0
end +
case when convert(varchar(5),t.end_time_temp,114)