我有一个挑战是报告给定日期范围内每个特定周的学生出勤情况。因此,我目前可以提取每周的每周出勤率,但它在一周的几天内总计(汇总),因此,如果有4周和4个星期一(即每周一个星期一),我会得到4个星期一但不是每周显示
我当前的表格涉及:
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
...
我当前的sql(给定日期范围'2017-06-25'和'2017-07-20'以及给定的'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;