TSQL计算时间段内连续缺勤的行数
问题:我试图计算每个学生在一周内连续缺课的次数 e、 g.如果MATH1234班在第1天第4期和第4天第3期上课,学生0012345在第1周第1期和第4天第3期缺课,则与该学生在第1周第4期和第2周第1期缺课相同 我有一个名为“课程”的表格,其中包含所有学生和他们注册的班级的运行列表,以及他们是否缺课: 课程[学生ID],[课程编号],[行号],[学期],[学年],[学期],[周],[日期],[上课日期],[Isassent],[原因代码],[连续作业] 我需要计算一个学生在一周内连续缺课的每一个班级的连续缺课次数,见上面的解释 鉴于: 学生001234在课程日期28/3的班级编号1CVASX11的连续成绩将为1,因为该日期之前的缺勤是一周多前的16/3。同样,上课日期9/3的连续成绩为2,因为该学生也在一周时间内的7/3缺席 我目前所做的是更新Lessons表,更改ConcertiveAbs的值,如下所示:TSQL计算时间段内连续缺勤的行数,tsql,subquery,Tsql,Subquery,问题:我试图计算每个学生在一周内连续缺课的次数 e、 g.如果MATH1234班在第1天第4期和第4天第3期上课,学生0012345在第1周第1期和第4天第3期缺课,则与该学生在第1周第4期和第2周第1期缺课相同 我有一个名为“课程”的表格,其中包含所有学生和他们注册的班级的运行列表,以及他们是否缺课: 课程[学生ID],[课程编号],[行号],[学期],[学年],[学期],[周],[日期],[上课日期],[Isassent],[原因代码],[连续作业] 我需要计算一个学生在一周内连续缺课的每一
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
我需要在那个里设置一个时间段,这样它只在上课日期的一周内累计。有人知道吗?我认为您丢失的一件物品限制了计数,仅包括缺勤时间,如果缺勤时间低于原缺勤时间的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
那太好了!:我觉得我把事情弄得太复杂了:在同一个问题上,有没有办法记录连续缺席5次或以上的次数?如果学生缺席2节课,然后出现1节课,然后错过接下来的3节课,这只算作连续缺席2节和3节,而不是5节。
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
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