[英]Calculating duration between two times that occur within a specific window
I'm trying to calculate the duration between a START_TIME and an END_TIME where they occur within set windows of time with SQL Server 2016.我正在尝试使用 SQL Server 2016 计算 START_TIME 和 END_TIME 之间的持续时间,其中它们发生在设定的 windows 时间范围内。
I have a set of data that looks like this:我有一组看起来像这样的数据:
| 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 |
The above output can be achieved with the following query:上面的 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
;
I want to calculate the duration for each row that occurs within WINDOW_1 and WINDOW_2.我想计算在 WINDOW_1 和 WINDOW_2 中发生的每一行的持续时间。 My desired output would add WINDOW_1_DURATION and WINDOW_2_DURATION columns.我想要的 output 将添加 WINDOW_1_DURATION 和 WINDOW_2_DURATION 列。 As an example, row two would have a WINDOW_1_DURATION of 60.例如,第二行的 WINDOW_1_DURATION 为 60。
Ideally I'd like to achieve this without using functions or the creation of new tables.理想情况下,我想在不使用函数或创建新表的情况下实现这一点。
Edit:编辑:
The desired output would look like this:所需的 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 |
Edit 2: As an explanation for my example of the WINDOW_1_DURATION being 60 for row 2, this is a measurement of the minutes between the START_TIME of 16:00 and the END_TIME of 18:00 that occur with the WINDOW_1_START of 17:00 and the WINDOW_1_END of 20:00.编辑 2:作为我对第 2 行的 WINDOW_1_DURATION 为 60 的示例的解释,这是对 16:00 的 START_TIME 和 18:00 的 END_TIME 之间的分钟数的测量,该时间发生在 17:00 的 WINDOW_1_START 和WINDOW_1_END 20:00。
Below query will deliver your desired output: (Logic: First I have CAST start_time and end_time as time in starttiem and endtime column. Then with Case I have tried to identify how overlapping happend. For example if WINDOW_1_START is less then starttime and WINDOW_1_END IS BETWEEN starttime and endtime then the overlapped time is between starttime and Window_1_End etc.)下面的查询将提供您想要的 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.