0

I have a challenge to report on attendance for students per unique week per day for a given date range. So I currently can extract weekly attendance per day but it summed (aggregated) over the days of the week, so that if there were 4 weeks and 4 Mondays (ie. 1 Monday per week) I get the 4 Mondays but not shown per week.

My current tables involved:

Attendance Table
--------------------- 
Attendance_Identifier 
Student_Identifier 
Classroom_Identifier 
Attendance_Datetime 
Attendance_Value 
...
Student
------------------
Student_Identifier
Person_Identifier
Classroom_Identifier
...

Person
-----------------
Person_Identifier
Frist_Name
Last_Name
Gender
...

My current sql (given a date range of '2017-06-25' and '2017-07-20' and a given class of '365'):

SELECT 
concat(p.Frist_Name, ' ',p.Last_Name) AS Student, p.Gender
,cast(COUNT(CASE WHEN weekday(date(Attendance_Datetime)) = 0
then 1 END) AS CHAR) AS Mon
,cast(COUNT(CASE WHEN weekday(date(Attendance_Datetime)) = 1
then 1 END) AS CHAR) AS Tue
,cast(COUNT(CASE WHEN weekday(date(Attendance_Datetime)) = 2
then 1 END) AS CHAR) AS Wed
,cast(COUNT(CASE WHEN weekday(date(Attendance_Datetime)) = 3
then 1 END) AS CHAR) AS Thu
,cast(COUNT(CASE WHEN weekday(date(Attendance_Datetime)) = 4
then 1 END) AS  CHAR) AS Fri
,cast(COUNT(distinct date(Attendance_Datetime)) AS  CHAR) AS WeeklyTotal
FROM (
SELECT distinct date(Attendance_Datetime) as Attendance_Date,a.* 
FROM Attendance a WHERE date(Attendance_Datetime) BETWEEN '2017-06-25' AND '2017-07-20'
AND a.Classroom_Identifier = 365 AND a.Course_Identifier IS NULL
AND (Attendance_Value = 'Present' OR Attendance_Value = 'Late')
AND weekday(date(Attendance_Datetime)) not in (5,6) GROUP BY Student_Identifier,Attendance_Date
) a
JOIN Student s ON s.Student_Identifier=a.Student_Identifier
JOIN Person p ON p.Person_Identifier=s.Person_Identifier
WHERE (Attendance_Value = 'Present' OR Attendance_Value = 'Late')
AND (p.Gender = 'Male' OR p.Gender = 'Female')
GROUP BY Student, Gender ORDER BY p.Gender, Student, Mon DESC;

enter image description here

luhfluh
  • 487
  • 2
  • 8
  • 26
  • 1
    Consider handling issues of data display in application code – Strawberry Jul 12 '17 at 21:13
  • Thanks @strawberry for your suggestion, but I am writing this as a stored procedure to just display the results without any other application to render the results (except the results exported in csv or excel) – luhfluh Jul 12 '17 at 21:19

0 Answers0