所以我目前正在等待我的查询运行(大约需要 5 分钟)
我开发了一个查询,它将查看学生的出勤记录,识别一系列出勤代码并根据日期和上课时间对它们进行分组。
如果我试图找到特定学生的最高缺勤次数,则此查询非常有用,但当我尝试创建一个显示所有活跃学生的最高缺勤次数的表时,它变得更加棘手。
为此,我使用游标将 StudentNo(唯一 ID)分配给参数,然后运行我的原始查询,并将结果放入名为 @results 的临时表中。
这是我的代码:
DECLARE @StudentId INT
DECLARE @getStudentId CURSOR
DECLARE @Results TABLE(StudentNo INT,AttendanceCode VarChar(2),StartDate DateTime,EndDate DateTime,"# of Classes" INT)
SET @getStudentId = CURSOR FOR
SELECT StudentNo
FROM [dbo].[Students]
OPEN @getStudentId
FETCH NEXT
FROM @getStudentId INTO @StudentId
WHILE @@FETCH_STATUS = 0
BEGIN
WITH AttendanceCodeMaster AS
(SELECT
[dbo].[Students].StudentNo,
CAST(CONVERT(date,[dbo].[CourseOfferingSchedule].ClassDate,101) as DATETIME) + CAST(CONVERT(time,dbo.CourseOfferingSchedule.ClassStartTime,101) AS DATETIME) as ClassDate,
[dbo].[CourseOfferingAttendanceScheduled].AttendanceCode
FROM [dbo].[CourseOfferingAttendanceScheduled]
INNER JOIN [dbo].[Students] on [dbo].[CourseOfferingAttendanceScheduled].StudentNo = [dbo].[Students].StudentNo
INNER JOIN dbo.[CourseOfferingSchedule] on [dbo].[CourseOfferingAttendanceScheduled].ScheduleID = [dbo].[CourseOfferingSchedule].ScheduleID
INNER JOIN [dbo].[StudentStatus] on [dbo].[Students].StudentStatusID = [dbo].[StudentStatus].StudentStatusID
where
[dbo].[Students].StudentNo = @StudentId and StudentStatus = 'Active' and Complete = 'Y'
),
RunGroup AS
(SELECT StudentNo,ClassDate, AttendanceCode, (SELECT COUNT(*) From AttendanceCodeMaster as G WHERE G.AttendanceCode <> GR.AttendanceCode AND G.ClassDate <= GR.ClassDate) as RunGroup
FROM AttendanceCodeMaster as GR ),
AbsenceStreaks AS
(SELECT
StudentNo,
AttendanceCode,
MIN(ClassDate) as StartDate,
MAX(ClassDate)as EndDate,
COUNT(*) as '# of Classes'
FROM RunGroup
where AttendanceCode = 'A'
GROUP BY StudentNo,AttendanceCode, RunGroup),
LongestStreak AS
(SELECT TOP 1 * FROM AbsenceStreaks
Order BY '# of Classes' Desc)
INSERT INTO @Results SELECT * FROM LongestStreak
FETCH NEXT
FROM @getStudentId INTO @StudentId
END
CLOSE @getStudentId
DEALLOCATE @getStudentId
SELECT * from @Results
where "# of Classes" >= 30
order by StudentNo