1

我试图找出人们在 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 重新开始。

4

3 回答 3

2

这类似于约翰的答案,但更简单一些。

您可以通过减去一系列数字来识别相邻行的组 - 差异是恒定的。所以:

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;
于 2020-02-12T00:58:38.043 回答
2

只是另一种选择......非常类似于 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
于 2020-02-12T00:52:18.527 回答
1

您可以先lag()检查每个员工的前一行(按 排序work_period)是否与当前行正好有天数。在条件为真CASE时返回的表达式中使用它,否则。然后使用 的窗口版本按 的顺序总结每个员工的s 和s 。这为每个员工的每组连续天数提供了一个数字。然后,您可以使用此组号在窗口版本中为用户添加按 .排序的分区中的每一行。00sum()01work_periodPARTITION BYsum()1work_period

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;

db<>小提琴

于 2020-02-12T00:47:40.760 回答