【发布时间】:2020-09-27 04:46:30
【问题描述】:
我正在尝试自学滞后和领先功能,并认为我会尝试使用以下报告,但我运气不佳。我的目标是为一个部门记录一些随叫随到的开始和停止时间,并创建一份报告,详细说明没有覆盖的那一天的时间间隔。假设该部门每周 7 天、每天 24 小时提供服务。处理此问题的唯一方法是将其加入具有可用日期和每一分钟的日期时间表吗?任何建议将不胜感激。
以下数据的预期结果是:
on 09/01/2020 dept 3042300031 had a time gap from 20:59 to 21:00 and a time gap from 22:59 to 23:59
on 09/02/2020 dept 3042300031 had a time gap from 00:00 to 00:05 and a time gap from 20:59 to 22:00 and a time gap from 22:50 to 23:59
on 09/03/2020 dept 3042300031 had a time gap from 00:00 to 23:59
on 09/04/2020 dept 3042300031 had a time gap from 20:59 to 23:59
IF OBJECT_ID('tempdb..#report') IS NOT NULL DROP TABLE #report
GO
CREATE TABLE #report (
Contact_Date date
,Line int
,Start_Instant_dttm smalldatetime
,End_Instant_dttm datetime
,Asgn_to_Role int
,Asgn_to_Team bigint
);
INSERT INTO #report
SELECT
'9/1/2020',1,'9/1/2020 00:00','9/1/2020 20:59',270,3042300031
UNION
SELECT
'9/1/2020',2,'9/1/2020 21:00','9/1/2020 22:59',270,3042300031
UNION
SELECT
'9/2/2020',1,'9/2/2020 00:05','9/2/2020 20:59',270,3042300031
UNION
SELECT
'9/2/2020',2,'9/2/2020 22:00','9/2/2020 22:59',270,3042300031
UNION
SELECT
'9/4/2020',1,'9/4/2020 00:00','9/4/2020 20:59',270,3042300031;
【问题讨论】:
-
请添加预期结果应该是什么样子以及 24:00 是什么时候?
-
到目前为止你有什么尝试?
-
是的,2400 上的大脑放屁将其更改为 2359。就我到目前为止所尝试的而言。我尝试了类似于 dnoeth 的第一个 cte 的方法,我得到了 on call 之间的时间间隔,但在日历天数方面遇到了问题;因此我提到了加入日期时间表。哪个 dnoeth 似乎只加入了一个日期表。
标签: sql sql-server-2012