繁体   English   中英

计算特定 window 内发生的两次之间的持续时间

[英]Calculating duration between two times that occur within a specific window

我正在尝试使用 SQL Server 2016 计算 START_TIME 和 END_TIME 之间的持续时间,其中它们发生在设定的 windows 时间范围内。

我有一组看起来像这样的数据:

| USER| START_TIME       | END_TIME         | DURATION | WINDOW_1_START | WINDOW_1_END | WINDOW_2_START | WINDOW_2_END |
|-----|------------------|------------------|----------|----------------|--------------|-----------------|--------------|
| jim | 2021-01-24 14:00 | 2021-01-24 16:00 | 120      | 17:00          | 20:00        | 20:00           | 22:00        |
| jim | 2021-01-24 16:00 | 2021-01-24 18:00 | 120      | 17:00          | 20:00        | 20:00           | 22:00        | 
| jim | 2021-01-24 18:00 | 2021-01-24 19:30 | 90       | 17:00          | 20:00        | 20:00           | 22:00        |
| jim | 2021-01-24 19:30 | 2021-01-24 22:00 | 150      | 17:00          | 20:00        | 20:00           | 22:00        |

上面的 output 可以通过以下查询来实现:

SELECT USER,
       START_TIME,
       END_TIME,
       DATEDIFF ( minute, START_TIME, END_TIME ) AS DURATION
       CAST ( '17:00' AS TIME ) AS WINDOW_1_START,
       CAST ( '20:00' AS TIME ) AS WINDOW_1_END,
       CAST ( '20:00' AS TIME ) AS WINDOW_2_START,
       CAST ( '22:00' AS TIME ) AS WINDOW_2_END
FROM EVENTS
;

我想计算在 WINDOW_1 和 WINDOW_2 中发生的每一行的持续时间。 我想要的 output 将添加 WINDOW_1_DURATION 和 WINDOW_2_DURATION 列。 例如,第二行的 WINDOW_1_DURATION 为 60。

理想情况下,我想在不使用函数或创建新表的情况下实现这一点。

编辑:

所需的 output 如下所示:

| USER| START_TIME       | END_TIME         | DURATION | WINDOW_1_START | WINDOW_1_END | WINDOW_2_START | WINDOW_2_END | WINDOW_1_DURATION | WINDOW_2_DURATION |
|-----|------------------|------------------|----------|----------------|--------------|-----------------|--------------|---------------------------------------|
| jim | 2021-01-24 14:00 | 2021-01-24 16:00 | 120      | 17:00          | 20:00        | 20:00           | 22:00        | 0                 | 0                 |
| jim | 2021-01-24 16:00 | 2021-01-24 18:00 | 120      | 17:00          | 20:00        | 20:00           | 22:00        | 60                | 0                 |
| jim | 2021-01-24 18:00 | 2021-01-24 19:30 | 90       | 17:00          | 20:00        | 20:00           | 22:00        | 90                | 0                 |
| jim | 2021-01-24 19:30 | 2021-01-24 22:00 | 150      | 17:00          | 20:00        | 20:00           | 22:00        | 30                | 120               |

编辑 2:作为我对第 2 行的 WINDOW_1_DURATION 为 60 的示例的解释,这是对 16:00 的 START_TIME 和 18:00 的 END_TIME 之间的分钟数的测量,该时间发生在 17:00 的 WINDOW_1_START 和WINDOW_1_END 20:00。

下面的查询将提供您想要的 output :(逻辑:首先我在 starttiem 和 endtime 列中有CAST start_time 和 end_time 作为时间。然后用案例我试图确定重叠是如何发生的。例如,如果 WINDOW_1_START 小于 starttime 并且 WINDOW_1_END IS BETWEEN starttime 和 endtime 然后重叠时间在 starttime 和 Window_1_End 等之间)

with cte as (
SELECT [USER],
       START_TIME,
       END_TIME,
       DATEDIFF ( minute, START_TIME, END_TIME ) AS DURATION,
       CAST ( '17:00' AS TIME ) AS WINDOW_1_START,
       CAST ( '20:00' AS TIME ) AS WINDOW_1_END,
       CAST ( '20:00' AS TIME ) AS WINDOW_2_START,
       CAST ( '22:00' AS TIME ) AS WINDOW_2_END,
       cast(start_time as time)starttime,
       cast(end_time as time)endtime       
FROM EVENTS)

select * ,
case when (WINDOW_1_START between starttime and endtime and WINDOW_1_end between starttime  and endtime  ) then (datediff(minute,WINDOW_1_START,WINDOW_1_end)) 
when (WINDOW_1_START between starttime and endtime and WINDOW_1_END > endtime)  then (datediff(minute,WINDOW_1_START,endtime)) 
when (WINDOW_1_START<starttime and WINDOW_1_END between starttime and endtime) then (datediff(minute,starttime,WINDOW_1_END))
when (WINDOW_1_START<starttime and WINDOW_1_END >endtime) then (datediff(minute,starttime,endtime)) end WINDOW_1_DURATION,
case when (WINDOW_2_START between starttime and endtime and WINDOW_2_end between starttime  and endtime  ) then (datediff(minute,WINDOW_2_START,WINDOW_2_end)) 
when (WINDOW_2_START between starttime and endtime and WINDOW_2_END > endtime)  then (datediff(minute,WINDOW_2_START,endtime)) 
when (WINDOW_2_START<starttime and WINDOW_2_END between starttime and endtime) then (datediff(minute,starttime,WINDOW_2_END))
when (WINDOW_2_START<starttime and WINDOW_2_END >endtime) then (datediff(minute,starttime,endtime)) end WINDOW_2_DURATION

from cte

Output: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM