D
dochsm
This query processes 200,000 records each week. It calculates two
cumulative attendance totals by weeknumber for students who are
identified by their admission number (Adno). Trouble is it takes
around 45 minutes to run. Is there a better way to write it?
Howard
INSERT
INTO Tbl_CumulativeAttendance
(
Adno ,
ExportWeekNumber ,
Used_Surname ,
CumulativePresentNumber ,
CumulativePossibleNumber,
CumulativePercent
)
SELECT DISTINCT
tmp_attends.Adno ,
tmp_attends.ExportWeekNumber,
tmp_attends.Used_Surname ,
(SELECT SUM(presentMark)
FROM tbl_TEMP_all_attendances_with_week_number
WHERE AdNo = tmp_attends.Adno
AND val( ExportWeekNumber) <=
val(tmp_attends.ExportWeekNumber)
) AS CumulativePresentNumber,
(SELECT SUM(PossibleMark)
FROM tbl_TEMP_all_attendances_with_week_number
WHERE AdNo = tmp_attends.Adno
AND val(ExportWeekNumber) <=
val(tmp_attends.ExportWeekNumber)
) AS CumulativePossibleNumber,
IIf([CumulativePossibleNumber]>0,
([CumulativePresentNumber]/[CumulativePossibleNumber])*100,
0) AS CumulativePercent
FROM tbl_TEMP_all_attendances_with_week_number AS tmp_attends
ORDER BY
tmp_attends.Adno,
tmp_attends.ExportWeekNumber;
cumulative attendance totals by weeknumber for students who are
identified by their admission number (Adno). Trouble is it takes
around 45 minutes to run. Is there a better way to write it?
Howard
INSERT
INTO Tbl_CumulativeAttendance
(
Adno ,
ExportWeekNumber ,
Used_Surname ,
CumulativePresentNumber ,
CumulativePossibleNumber,
CumulativePercent
)
SELECT DISTINCT
tmp_attends.Adno ,
tmp_attends.ExportWeekNumber,
tmp_attends.Used_Surname ,
(SELECT SUM(presentMark)
FROM tbl_TEMP_all_attendances_with_week_number
WHERE AdNo = tmp_attends.Adno
AND val( ExportWeekNumber) <=
val(tmp_attends.ExportWeekNumber)
) AS CumulativePresentNumber,
(SELECT SUM(PossibleMark)
FROM tbl_TEMP_all_attendances_with_week_number
WHERE AdNo = tmp_attends.Adno
AND val(ExportWeekNumber) <=
val(tmp_attends.ExportWeekNumber)
) AS CumulativePossibleNumber,
IIf([CumulativePossibleNumber]>0,
([CumulativePresentNumber]/[CumulativePossibleNumber])*100,
0) AS CumulativePercent
FROM tbl_TEMP_all_attendances_with_week_number AS tmp_attends
ORDER BY
tmp_attends.Adno,
tmp_attends.ExportWeekNumber;