根据每日粒度的事实表计算日期间隔

时间:2018-08-09 14:12:15

标签: sql sql-server

我有一些转换后得到的学生缺席数据。数据是每天的:

WITH datasample AS (
    SELECT 1 AS StudentID, 20180101 AS DateID, 0 AS AbsentToday, 0 AS AbsentYesterday UNION ALL
    SELECT 1, 20180102, 1, 0 UNION ALL
    SELECT 1, 20180103, 1, 1 UNION ALL
    SELECT 1, 20180104, 1, 1 UNION ALL
    SELECT 1, 20180105, 1, 1 UNION ALL
    SELECT 1, 20180106, 0, 1 UNION ALL
    SELECT 2, 20180101, 0, 0 UNION ALL
    SELECT 2, 20180102, 1, 0 UNION ALL
    SELECT 2, 20180103, 1, 1 UNION ALL
    SELECT 2, 20180104, 0, 1 UNION ALL
    SELECT 2, 20180105, 1, 0 UNION ALL
    SELECT 2, 20180106, 1, 1 UNION ALL
    SELECT 2, 20180107, 0, 1
)
SELECT *
FROM datasample
ORDER BY StudentID, DateID

我需要添加一列(AbsencePeriodInMonth),该列将计算学生在该月的缺勤时间。 例如,当月一个连续的时段中没有StudentID = 1,而StudentID = 2则有两个时段,如下所示:

StudentID DateID    AbsentToday AbsentYesterday AbsencePeriodInMonth
1         20180101  0           0               0
1         20180102  1           0               1
1         20180103  1           1               1
1         20180104  1           1               1
1         20180105  1           1               1
1         20180106  0           1               0
2         20180101  0           0               0
2         20180102  1           0               1
2         20180103  1           1               1
2         20180104  0           1               0
2         20180105  1           0               2
2         20180106  1           1               2
2         20180107  0           1               0

我的目标实际上是计算事实表中每一天之前的连续缺席天,我认为如果得到AbsencePeriodInMonth列,可以将其添加到*:之后的查询中,从而做到这一点。

,CASE WHEN AbsentToday = 1 THEN DENSE_RANK() OVER(PARTITION BY StudentID, AbsencePeriodInMonth ORDER BY DateID)
           ELSE 0
     END AS DaysAbsent

关于如何添加AbsencePeriodInMonth或以其他方式计算连续缺勤天数的想法吗?

2 个答案:

答案 0 :(得分:1)

使用Recursive CTEDense_Rank

WITH datasample AS (
    SELECT 1 AS StudentID, 20180101 AS DateID, 0 AS AbsentToday, 0 AS AbsentYesterday UNION ALL
    SELECT 1, 20180102, 1, 0 UNION ALL
    SELECT 1, 20180103, 1, 1 UNION ALL
    SELECT 1, 20180104, 1, 1 UNION ALL
    SELECT 1, 20180105, 1, 1 UNION ALL
    SELECT 1, 20180106, 0, 1 UNION ALL
    SELECT 2, 20180101, 0, 0 UNION ALL
    SELECT 2, 20180102, 1, 0 UNION ALL
    SELECT 2, 20180103, 1, 1 UNION ALL
    SELECT 2, 20180104, 0, 1 UNION ALL
    SELECT 2, 20180105, 1, 0 UNION ALL
    SELECT 2, 20180106, 1, 1 UNION ALL
    SELECT 2, 20180107, 0, 1
), cte as
(Select *,DateID as dd 
from datasample 
where AbsentToday = 1 and AbsentYesterday = 0

union all

Select d.*, c.dd 
from datasample d
join cte c
on d.StudentID = c.StudentID and d.DateID = c.DateID + 1 
    where d.AbsentToday = 1
), cte1 as
(
Select *, DENSE_RANK() over (partition by StudentId order by dd) as de 
from cte
)
Select d.*, IsNull(c.de,0) as AbsencePeriodInMonth
from cte1 c 
right join datasample d
on d.StudentID = c.StudentID and c.DateID = d.DateID
order  by d.StudentID, d.DateID

答案 1 :(得分:1)

您可以通过计算事前的0来识别每个周期。然后,您可以使用dense_rank()枚举它们。

select ds.*,
       (case when absenttoday = 1 then dense_rank() over (partition by studentid order by grp)
             else 0
        end) as AbsencePeriodInMonth
from (select ds.*, sum(case when absenttoday = 0 then 1 else 0 end) over (partition by studentid order by dateid) as grp
      from datasample ds
     ) ds
order by StudentID, DateID;

Here是一个SQL提琴。