I am trying to query 3 tables:
classroom:
id | name
----------------
1 | bob's class
student_enrollment:
studentId | classroomId
-----------------------
1 | 1
student_progress:
studentId | dateTime
---------------------
1 | 2014-08-15 09:33:23
and I am trying to end up with a result such as:
classroomName | studentId | days
------------------------------------------
Bob's class | 1 | 112
Where days is the distinct number of days the particular student had a dateTime entry.
SELECT classroom.name, student_enrollment.studentId, student_progress.dateTime
FROM classroom
JOIN student_enrollment ON student_enrollment.classroomId = classroom.id
JOIN student_progress ON student_progress.studentId = student_enrollment.studentId
WHERE `dateTime` >= '2014-08-01 09:00:34'
ORDER BY classroom.name
Gives me a table with every dateTime entry for each student. Any way I've tried to add a count to the query gives me at best a result with just one student and the total count of distinct dateTime days.
SELECT classroom.name, student_enrollment.studentId, COUNT(DISTINCT YEAR(student_progress.dateTime),MONTH(student_progress.dateTime),DAY(student_progress.dateTime)) AS days
FROM classroom
JOIN student_enrollment ON student_enrollment.classroomId = classroom.id
JOIN student_progress ON student_progress.studentId = student_enrollment.studentId
WHERE `dateTime` >= '2014-08-01 09:00:34'
ORDER BY classroom.name
I've tried moving the count to a sub-query in the join portion, and a variety of other things, but just end up with errors such as unknown table student_progress and just can't seem to land on the right way to put the query together to get the distinct count of dateTime entries for each studentId.
dateTimevalues? - Madbreaks