Reputation: 2378
I am looking to count consecutive day spells for each individual person.
My tables:
CREATE TABLE Absence(
Date Date,
Code varchar(10),
Name varchar(10),
Type varchar(10)
);
INSERT INTO Absence (Date, Code, Name, Type)
VALUES ('01-10-18', 'S', 'Sam', 'Sick'),
('01-11-18','S', 'Sam', 'Sick'),
('01-12-18','S', 'Sam', 'Sick'),
('01-21-18','S', 'Sam', 'Sick'),
('01-26-18','S', 'Sam', 'Sick'),
('01-27-18','S', 'Sam', 'Sick'),
('02-12-18','S', 'Sam', 'Holiday'),
('02-13-18','S', 'Sam', 'Holiday'),
('02-18-18','S', 'Sam', 'Holiday'),
('02-25-18','S', 'Sam', 'Holiday'),
('02-10-18','S', 'Sam', 'Holiday'),
('02-13-18','F', 'Fred', 'Sick'),
('02-14-18','F', 'Fred', 'Sick'),
('02-17-18','F', 'Fred', 'Sick'),
('02-25-18','F', 'Fred', 'Sick'),
('02-28-18','F', 'Fred', 'Sick');
This is the code i currently have:
WITH CTE AS
(
SELECT
Date,
Name,
Type
,GroupingSet = DATEADD(DAY, ROW_NUMBER() OVER
(PARTITION BY [Name], [Type] ORDER BY [Date]), [Date])
FROM Absence
)
SELECT
Name,
StartDate = MIN(Date),
EndDate = MAX(Date),
Result = COUNT(Name),
min(Type) AS [Type]
FROM CTE
GROUP BY Name, GroupingSet
-- HAVING COUNT(NULLIF(Code, 0)) > 1
ORDER BY Name, StartDate
Which produces the result:
| Name | StartDate | EndDate | Result | Type |
|------|------------|------------|--------|---------|
| Fred | 2018-02-13 | 2018-02-13 | 1 | Sick |
| Fred | 2018-02-14 | 2018-02-14 | 1 | Sick |
| Fred | 2018-02-17 | 2018-02-17 | 1 | Sick |
| Fred | 2018-02-25 | 2018-02-25 | 1 | Sick |
| Fred | 2018-02-26 | 2018-02-28 | 1 | Sick |
| Sam | 2018-01-10 | 2018-01-10 | 1 | Sick |
| Sam | 2018-01-11 | 2018-01-11 | 1 | Sick |
| Sam | 2018-01-12 | 2018-01-12 | 1 | Sick |
| Sam | 2018-01-21 | 2018-01-21 | 1 | Sick |
| Sam | 2018-01-26 | 2018-01-26 | 1 | Sick |
| Sam | 2018-01-27 | 2018-01-27 | 1 | Sick |
| Sam | 2018-02-10 | 2018-02-10 | 1 | Holiday |
| Sam | 2018-02-12 | 2018-02-12 | 1 | Holiday |
| Sam | 2018-02-13 | 2018-02-13 | 1 | Holiday |
| Sam | 2018-02-18 | 2018-02-18 | 1 | Holiday |
| Sam | 2018-02-25 | 2018-02-25 | 1 | Holiday |
Where as i am looking for a result set like this:
| Name | Date | Result | Type |
|------|------------|---------|---------|
| Fred | 2018-02-13 | 2 | Sick |
| Sam | 2018-01-27 | 2 | Sick |
| Sam | 2018-02-10 | 1 | Holiday |
I need to count the consecutive days where there is more then 1 day in a row. And then have this as a total of how many consecutive spells someone had. e.g. fred had 2 consecutive sick spells during that time period. I also need this to cover if someone had a friday and a monday off, this should count as a consecutive spell.
Im a bit lost as to how to get there. Any help would be appreciate.
PLEASE SEE : http://sqlfiddle.com/#!18/88612/16
Upvotes: 2
Views: 4987
Reputation: 2378
SELECT
s.[Name], 'Last 12 Months' as [Date], s.[Type], COUNT(s.[numdays]) AS
[Consecutive Spells]
FROM ( select name, min(date) AS [Date], max(date) AS [Date2],
count(*) as numdays, type
from (select a.*, row_number() over (partition by name, type order by date)
as seqnum_ct
from absence a
) a
group by name, type, dateadd(day, -seqnum_ct, date)
HAVING count(*) > 1
)S
GROUP BY s.[Name], s.[Type]
This is more the result i was looking for. - http://sqlfiddle.com/#!18/472d2/23
But thanks for your help Gordon, it pushed me in the right direction!
Upvotes: -1
Reputation: 1269503
You can get the periods of absences using:
select name, min(date), max(date), count(*) as numdays, type
from (select a.*,
row_number() over (partition by name, type order by date) as seqnum_ct
from absence a
) a
group by name, type, dateadd(day, -seqnum_ct, date);
Here is a SQL Fiddle for this.
You can add having count(*) > 1
to get periods with one day or more. This seems useful. I don't understand what the ultimate output is. The description just doesn't make sense to me.
If you want the number of absences that are 2 or more days, then use this as a subquery/CTE:
select name, count(*), type
from (select name, min(date) as mindate, max(date) as maxdate, count(*) as numdays, type
from (select a.*,
row_number() over (partition by name, type order by date) as seqnum_ct
from absence a
) a
group by name, type, dateadd(day, -seqnum_ct, date)
) b
where numdays > 1
group by name, type;
Upvotes: 4
Reputation: 37337
Try this:
select name, min([date]) [date], count(*) [result], [type] from (
select *, SUM(isConsecutive) over (partition by name,[type] order by [date] rows between unbounded preceding and current row) [isConsecutiiveId]
from (
select *, case when dateadd(day, -1, [date]) = LAG([date]) over (partition by name,[type] order by [date]) then 0 else 1 end [isConsecutive] from #Absence
) a
) a group by name,[type],isConsecutiiveId
It results more periods of absence than your expected result, as in your data there's more periods of absence. It obviously includes your results, but there's more :)
Upvotes: 1