Reputation: 897
I'm trying to find how many days people have continuously worked in SQL. I'm thinking a rolling sum might be the solution but don't know how to work it out.
My sample data is
| 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 |
The designated result should be
| 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 |
If the days are not continuous, the continuous counting will re-start from 1.
Upvotes: 1
Views: 228
Reputation: 1269623
This is similar to John's answer but a bit simpler.
You can identify groups of adjacent rows by subtracting a sequence of numbers -- the difference is constant. So:
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;
Another interesting way to do this is to identify the rows where the "islands" start and then use 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;
Upvotes: 2
Reputation: 81930
Just another option ... Very similar to a Gaps-and-Islands, but without the final aggregation.
Example
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
Returns
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
Upvotes: 2
Reputation: 37472
You can first use lag()
to check if the previous row (as sorted by work_period
) per employee has exactly day lees then the current row. Use that in a CASE
expression that returns 0
if the condition is true and 0
otherwise. Then use the windowed version of sum()
to sum up the 0
s and 1
s per employee in the order of work_period
. That gives you a number per group of continuous days for each employee. You can then use this group number to PARTITION BY
additionally to the user in a windowed version of sum()
adding 1
for each row in the partition ordered by work_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;
Upvotes: 1