簡體   English   中英

日期連續時的滾動總和

[英]Rolling Sum when date is continuous

我試圖找出人們在 SQL 中連續工作了多少天。我認為滾動總和可能是解決方案,但不知道如何解決。

我的示例數據是

| Employee | work_period |
| 1        | 2019-01-01  |
| 1        | 2019-01-02  |
| 1        | 2019-01-03  |
| 1        | 2019-01-04  |
| 1        | 2019-01-05  |
| 1        | 2019-01-10  |
| 1        | 2019-01-11  |
| 1        | 2019-01-12  |
| 2        | 2019-01-20  |
| 2        | 2019-01-22  |
| 2        | 2019-01-23  |
| 2        | 2019-01-24  |

指定的結果應該是

| Employee | work_period | Continuous Days |
| 1        | 2019-01-01  | 1               |
| 1        | 2019-01-02  | 2               |
| 1        | 2019-01-03  | 3               |
| 1        | 2019-01-04  | 4               |
| 1        | 2019-01-05  | 5               |
| 1        | 2019-01-10  | 1               |
| 1        | 2019-01-11  | 2               |
| 1        | 2019-01-12  | 3               |
| 2        | 2019-01-20  | 1               |
| 2        | 2019-01-22  | 1               |
| 2        | 2019-01-23  | 2               |
| 2        | 2019-01-24  | 3               |

如果天數不連續,則從1重新開始連續計數。

只是另一種選擇...非常類似於 Gaps-and-Islands,但沒有最終聚合。

例子

Select Employee
      ,work_period
      ,Cont_Days = row_number() over (partition by Employee,Grp Order by Work_Period)
 From  (
        Select *
              ,Grp = datediff(day,'1900-01-01',work_period) - row_number() over (partition by Employee Order by Work_Period) 
          From YourTable
       ) A

退貨

Employee    work_period Cont_Days
1           2019-01-01  1
1           2019-01-02  2
1           2019-01-03  3
1           2019-01-04  4
1           2019-01-05  5
1           2019-01-10  1
1           2019-01-11  2
1           2019-01-12  3
2           2019-01-20  1
2           2019-01-22  1
2           2019-01-23  2
2           2019-01-24  3

這類似於 John 的回答,但更簡單一些。

您可以通過減去一系列數字來識別相鄰行的組——差異是恆定的。 所以:

select Employee, work_period,
       row_number9) over (partition by employee, grp order by work_period) as day_counter
      ,Cont_Days = row_number() over (partition by Employee,Grp Order by Work_Period)
from (select t.*,
             dateadd(day,
                     - row_number() over (partition by employee order by work_period),
                     work_period
                    ) as grp
      from t
     ) t;

另一種有趣的方法是識別“島嶼”開始的行,然后使用datediff()

select t.*,
       datediff(day,
                max(case when island_start_flag = 1 then workperiod end) over (partition by employee order by workperiod),
                workperiod
               ) + 1 as days_counter
from (select t.*,
             (case when lag(workperiod) over (partition by employee order by workperiod) >= dateadd(day, -1, workperiod)
                   then 0 else 1
              end) as island_start_flag
      from t
     ) t;

您可以先使用lag()檢查每個員工的前一行(按work_period排序)是否恰好有 day lees,然后是當前行。 CASE表達式中使用它,如果條件為真則返回0 ,否則返回0 然后使用sum()的窗口版本按work_period的順序對每個員工的01求和。 這為您提供了每位員工每組連續天數。 然后,您可以在sum()的窗口版本中使用此組號對用戶進行PARTITION BY ,為按work_period排序的分區中的每一行添加1

SELECT employee,
       work_period,
       sum(1) OVER (PARTITION BY employee,
                                 g
                    ORDER BY work_period) continuous_days
       FROM (SELECT employee,
                    work_period,
                    sum(c) OVER (PARTITION BY employee
                                 ORDER BY work_period) g
                    FROM (SELECT employee,
                                 work_period,
                                 CASE
                                   WHEN lag(work_period) OVER (PARTITION BY employee
                                                               ORDER BY work_period) = dateadd(day, -1, work_period) THEN
                                     0
                                   ELSE
                                     1
                                 END c
                                 FROM elbat) x) y;

數據庫<>小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM