I am attempting to write a SQL query in Moodle that shows me when a student has enrolled as well as completed a specific course. If the Completion Date is null, then it's assumed that student hasn't completed the course. This is the kind of data I want to capture. This is what I have so far:
SELECT u.lastname, u.firstname, u.username, c.fullname,
FROM_UNIXTIME(cx.timecompleted) as "Completion Date",Null as "Enrol_Time"
FROM prefix_course_completions as cx
JOIN prefix_course as c on cx.course = c.id
Join prefix_user as u on cx.userid = u.id
Where cx.timecompleted is not null
and c.fullname like "%refresh%"
union
SELECT u.lastname, u.firstname, u.username, c.fullname,
FROM_UNIXTIME(cc.timecompleted) as "Completion Date", Null as "Enrol_Time"
From prefix_local_recompletion_cc_cc as cc
JOIN prefix_course as c on cc.course = c.id
Join prefix_user as u on cc.userid = u.id
and c.fullname like "%refresh%"
Where cc.timecompleted IS NOT NULL
union
SELECT u.lastname, u.firstname, u.username, co.fullname, Null as "Completion
Date",
FROM_UNIXTIME(ra.timemodified,'%D %M %h:%i:%s %x') AS enrol_time FROM
prefix_role_assignments ra,
prefix_user u, prefix_context c, prefix_course co
WHERE ra.contextid = c.id AND c.contextlevel = 50 AND ra.userid = u.id AND
c.instanceid = co.id AND ra.timemodified >0
and co.fullname like "%refresh%"
order by "Completion Date" DESC
The above query gives me all of the data I want (Student name, Course name, Enrollment / Completion Date). However as it stands, the Enrollment Date and Completion date are on separate rows.
Does anyone have any advice / insight on how I can get the Enrollment and Completion Dates on the same row of the table? I imagine it's some kind of JOIN query, but I am not sure.
Any help would be very much appreciated. Thanks.