簡體   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