SQL计算时间差时如何排除非工作时间
我要计算b.rDateTime与a.rDateTime之间的时间差,但想排除非工作时间和周末,例如:a.rDateTime等于2017-12-04 17:00:00.000,b.rDateTime等于2017-12-05 09:00:00.000,排除下班时间18:00-第二天09:00,即两个日期的时间差只是1个小时,请问SQL可以实现吗?请大神们来帮忙,谢谢!
select
r.id,
r.rNumber,
r.rTopics,
r.rContent,
r.rMemo,
r.rCreateDate,
r.rEndDate,
r.rRStatus,
r.rTaskPName,
a.Actions as shenhe,
a.rDateTime as shenhetime,
b.actions as shouli,
b.rDateTime as shoulitime,
(convert(varchar, b.rDateTime-a.rDateTime,108)) balance
from tbTaskRD r left join (select *,Row_Number() OVER (partition by rNumber ORDER BY rDateTime desc) seq from tbActions where Actions like ('(审批岗位)%') ) a on r.rNumber=a.rNumber
left join (select *,Row_Number() OVER (partition by rNumber ORDER BY rDateTime desc) seq from tbActions where Actions like('(受理岗位)%')) b on r.rNumber=b.rNumber
where r.rMethod='' and r.rCreateDate>('2017-01-01 00:00:00.000') and r.rRStatus='完成' and r.rTopics like ('%Support%')
select
r.id,
r.rNumber,
r.rTopics,
r.rContent,
r.rMemo,
r.rCreateDate,
r.rEndDate,
r.rRStatus,
r.rTaskPName,
a.Actions as shenhe,
a.rDateTime as shenhetime,
b.actions as shouli,
b.rDateTime as shoulitime,
(convert(varchar, b.rDateTime-a.rDateTime,108)) balance
from tbTaskRD r left join (select *,Row_Number() OVER (partition by rNumber ORDER BY rDateTime desc) seq from tbActions where Actions like ('(审批岗位)%') ) a on r.rNumber=a.rNumber
left join (select *,Row_Number() OVER (partition by rNumber ORDER BY rDateTime desc) seq from tbActions where Actions like('(受理岗位)%')) b on r.rNumber=b.rNumber
where r.rMethod='' and r.rCreateDate>('2017-01-01 00:00:00.000') and r.rRStatus='完成' and r.rTopics like ('%Support%')
...全文
相关推荐
SQL面试题:求时间差之和(有重复不计)
主要介绍了SQL面试题:求时间差之和(有重复不计),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
如何求两个日期的时间差(抛除节假日).人员表
如何求两个日期的时间差(抛除节假日).人员表
SQL DATEDIFF函数解析[项目源码]
本文详细解析了SQL中的DATEDIFF函数,包括其在不同数据库(如SQL Server、MySQL、PostgreSQL和SQLite)中的实现差异、语法及使用示例。文章还探讨了DATEDIFF在复杂场景中的应用,如计算工作日差、精确时间差等,并指出了其局限性,如不支持小数结果和忽略时间部分。最后,总结了DATEDIFF的优势和适用场景,为开发者提供了实用的参考。
经典T-SQL面试题
史上最好的SQL面试题,希望大家在面试前下载看看,谢谢
asp精妙的SQL语句例子
0.实现用一个表某些字段更新另一个表同名字段 update a set a.b=b.b,a.c=b.c from b where b.tindex=a.index 1.说明:复制表(只复制结构,源表名:a 新表名:b) SQL: select * into b from a where 1<>1 2.说明:拷贝表(拷贝数据,源表名:a 目标表名:b) SQL: insert into b(a, b, c) select d,e,f from a 3.说明:显示文章、提交人和最后回复时间 SQL: select a.title,a.username,b.adddate from ...


疑难问题
