Databases: Query to find who's written a note

Databases: Query to find who's written a note

by Mark Johnson -
Number of replies: 1
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
In my college we use moodle's notes to write UCAS references for each student.
The teacher for each of a student's classes writes a note against the student saying how they've performed throughout their time at college.
I need to pull out a report from the database (MySQL) showing who's written each note, and ideally, another showing how many each teacher has written.

Here's the query I've got at the moment:

select
 concat(s.firstname,' ',s.lastname) as student,
 p.content as reference,
 concat(t.firstname,' ',t.lastname) as teacher
from
 mdl_user as s,
 mdl_post as p,
 mdl_context as con,
 mdl_role_assignments as ass,
 mdl_user as t
where
 s.id = p.userid and
 p.courseid = con.instanceid and
 con.id = ass.contextid and
 ass.userid = t.id and
 p.module='notes' and
 p.created > unix_timestamp('2009-06-01')
order by
 s.lastname;
(the date restriction is to omit any notes written before teachers started doing references).

Unfortunately, this doesn't work as desired - currently, every note is linked to every teacher, returning about 6800 records (there are only around 300 references currently written). If anyone can help me with this, I'll be eternally grateful!
Average of ratings: -
In reply to Mark Johnson

Re: Databases: Query to find who's written a note

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I've managed to produce a reasonable report of how many notes have been made by each teacher. However, some are inaccurate for a reason I'm not entirely sure of. Many of the numbers it produces are spot on when I compare it to an individual query for the number of notes made by the user, however some are quite far over that number, giving me a total of about 100 phantom records. Here's the query I'm using, any feedback is appreciated.

SELECT concat(t.firstname,' ', t.lastname) as 'probable teacher', c.shortname as coursecode, c.id, count(p.id) as 'reference count'
 FROM mdl_user AS t 
 JOIN mdl_role_assignments as ass ON t.id=ass.userid 
JOIN mdl_context as con ON ass.contextid = con.id AND contextlevel= 50
JOIN mdl_course as c ON con.instanceid=c.id
JOIN mdl_post as p ON c.id=p.courseid
WHERE roleid = 3 AND 
p.module='notes' AND 
p.created>unix_timestamp('2009-06-01')
GROUP BY 'probable teacher';