连续计数SQL Server

时间:2018-03-15 11:10:57

标签: sql sql-server tsql

我希望为每个人计算连续一天的法术数。

我的桌子:

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');

这是我目前的代码:

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

产生结果:

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

我正在寻找像这样的结果集:

| Name |       Date | Result  |    Type |
|------|------------|---------|---------|
| Fred | 2018-02-13 |       2 |    Sick |
|  Sam | 2018-01-27 |       2 |    Sick |
|  Sam | 2018-02-10 |       1 | Holiday |

我需要计算连续超过1天的连续日数。然后将其作为总共有多少连续法术的人。例如弗雷德在那段时间内连续2次患病。我还需要这个来掩盖如果有人星期五和星期一休息,这应该算作一个连续的咒语。

我有点失去了如何到达那里。任何帮助将是欣赏。

请看:http://sqlfiddle.com/#!18/88612/16

3 个答案:

答案 0 :(得分:4)

您可以使用以下方式获得缺席时间:

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是一个SQL小提琴。

您可以添加having count(*) > 1以获得一天或更长时间的句点。这似乎很有用。我不明白最终的输出是什么。这个描述对我来说没有意义。

如果您想要2天或更多天的缺席次数,请将其用作子查询/ 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;

答案 1 :(得分:1)

试试这个:

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

与您的预期结果相比,它会导致更多的缺席时间,因为您的数据会有更多的缺席时间。它显然包括你的结果,但还有更多:)

答案 2 :(得分:-1)

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]

这是我正在寻找的结果。 - http://sqlfiddle.com/#!18/472d2/23

但是感谢你帮助戈登,它把我推向了正确的方向!