TSQL计算时间段内连续缺失的连续数

时间:2011-03-22 02:43:46

标签: tsql subquery

问题:我正在尝试计算每个学生在一周内为特定班级连续缺席的次数。

e.g。如果课程 MATH1234 第1天第4期 第4天第3期 上有课程 第1天第4期 第4天第3期 0012345 >在 第1周 中,与 第4天第3期 中缺席的学生相同em> 第1周 第1天第4期 第2周

我有一个名为课程的表,其中包含所有学生及其注册班级的运行列表,以及他们是否缺席任何班级:

课程([学生ID],[班级编号],[行号],[学期],[年],[学期],[周],[日期],[ ClassDate],[IsAbsent],[ReasonCode],[ConsecutiveAbs])

我需要计算一个学生对于他们所注册的每个班级的连续缺席次数,其中连续的abs在一周的时间内(参见上面的解释)。

鉴于:

Student ID    Class Number    Line Number Academic Period Year    Term    Week    Day Period  ClassDate               IsAbsent    ReasonCode  ConsecutiveAbs
001234        1CVASX11        1           1               2011    1       3       1       2011-02-14 00:00:00.000     1           U           0
001234        1CVASX11        1           1               2011    1       4       1       2011-02-21 00:00:00.000     1           U           0
001234        1CVASX11        1           1               2011    1       4       2       2011-02-23 00:00:00.000     1           U           0
001234        1CVASX11        1           1               2011    1       5       1       2011-02-28 00:00:00.000     1           U           0
001234        1CVASX11        1           1               2011    1       5       2       2011-03-02 00:00:00.000     1           U           0
001234        1CVASX11        1           1               2011    1       6       1       2011-03-07 00:00:00.000     1           U           0
001234        1CVASX11        1           1               2011    1       6       2       2011-03-09 00:00:00.000     1           U           0
001234        1CVASX11        1           1               2011    1       7       2       2011-03-16 00:00:00.000     1           U           0
001234        1CVASX11        1           1               2011    1       9       1       2011-03-28 00:00:00.000     1           U           0
001234        1CVASX61        6           1               2011    1       9       2       2011-03-28 00:00:00.000     1           U           0

ClassDate 28/3上的班级编号1CVASX11的学生001234的ConsecutiveAbs将为1,因为该日期之前的缺席是超过一周前的16/3。同样,ClassDate 9/3上的ConsecutiveAbs也是2,因为该学生在7/3也没有,这是在一周的时间内。

我目前正在做的是更新课程表,更改ConsecutiveAbs的值,如下所示:

UPDATE Lessons
SET ConsecutiveAbs = 
(SELECT ISNULL(SUM(CAST(IsAbsent AS numeric)), 0)
 FROM Lessons AS L3
 WHERE L3.IsAbsent = 1
 AND L1.IsAbsent <> 0
 AND L3.[Student ID] = L1.[Student ID]
 AND L3.[Class Number] = L1.[Class Number]
 AND L3.[Line Number] = L1.[Line Number]
 AND L3.[Year] = L1.[Year]
 AND L3.[ClassDate] <= L1.[ClassDate]
 AND (L3.[ClassDate] > (SELECT MAX(L2.ClassDate)
      FROM Lessons AS L2
      WHERE L2.IsAbsent = 0
      AND L2.[Student ID] = L1.[Student ID]
      AND L2.[Class Number] = L1.[Class Number]
      AND L2.[Line Number] = L1.[Line Number]
      AND L2.[Year] = L1.[Year]
      AND L2.ClassDate < L1.[ClassDate]
 ) OR (SELECT MAX(L2.ClassDate)
       FROM Lessons AS L2
       WHERE L2.IsAbsent = 0
       AND L2.[Student ID] = L1.[Student ID]
       AND L2.[Class Number] = L1.[Class Number]
       AND L2.[Line Number] = L1.[Line Number]
       AND L2.[Year] = L1.[Year]
       AND L2.ClassDate < L1.[ClassDate]
 ) IS NULL))
 FROM Lessons AS L1

但那给我这个:

001234  1CVASX11    1   1   2011    1   3   1   2011-02-14 00:00:00.000 1   U   1
001234  1CVASX11    1   1   2011    1   4   1   2011-02-21 00:00:00.000 1   U   2
001234  1CVASX11    1   1   2011    1   4   2   2011-02-23 00:00:00.000 1   U   3
001234  1CVASX11    1   1   2011    1   5   1   2011-02-28 00:00:00.000 1   U   4
001234  1CVASX11    1   1   2011    1   5   2   2011-03-02 00:00:00.000 1   U   5
001234  1CVASX11    1   1   2011    1   6   1   2011-03-07 00:00:00.000 1   U   6
001234  1CVASX11    1   1   2011    1   6   2   2011-03-09 00:00:00.000 1   U   7
001234  1CVASX11    1   1   2011    1   7   2   2011-03-16 00:00:00.000 1   U   8
001234  1CVASX11    1   1   2011    1   9   1   2011-03-28 00:00:00.000 1   U   9
001234  1CVASX61    6   1   2011    1   9   2   2011-03-28 00:00:00.000 1   U   9

我需要在那里设置一个时间段,所以它只在ClassDate的一周内加起来。有人有任何想法吗?

1 个答案:

答案 0 :(得分:1)

我认为你遗失的一件事就是把数量限制在仅包括缺席的情况下,如果它是在最初缺席的7天之内。

以下查询包含该条件,我相信它会为您提供所需的结果:

UPDATE LessonsAbsent
SET ConsecutiveAbs = (
    SELECT 
        ISNULL(SUM(CAST(IsAbsent AS numeric)), 0)
    FROM 
        Lessons RunningTotalAbsent 
    WHERE
        RunningTotalAbsent.IsAbsent = 1
        AND LessonsAbsent.[Student ID] = RunningTotalAbsent.[Student ID]
        AND LessonsAbsent.[Class Number] = RunningTotalAbsent.[Class Number]
        AND LessonsAbsent.[Line Number] = RunningTotalAbsent.[Line Number]
        AND LessonsAbsent.[Year] = RunningTotalAbsent.[Year]
        AND LessonsAbsent.ClassDate >= RunningTotalAbsent.ClassDate

        -- Only count as consecutive if the absence happened within under 7 days.
        AND DATEDIFF(DAY, RunningTotalAbsent.ClassDate, LessonsAbsent.ClassDate) < 7
    )
FROM Lessons LessonsAbsent 
WHERE LessonsAbsent.IsAbsent = 1