Useful SQL Queries?

Re: Useful SQL Queries?

by Saran Gokul -
Number of replies: 2

Here is a query to display the list of courses with its category and number of enrolments, number of inprogress and number of completions for each course.


SELECT min(base.id) AS id, course.fullname AS course_courselink, 

course.id AS course_id, category.name as category_name,

COUNT(base.status) AS course_completion_status, 

SUM(CASE WHEN base.status = 25 THEN 1 ELSE 0 END) AS course_completion_isinprogress, 

SUM(CASE WHEN base.status = 50 OR base.status = 75 THEN 1 ELSE 0 END) AS course_completion_iscomplete 

FROM mdl_course_completions base 

LEFT JOIN mdl_course course ON course.id = base.course 

LEFT JOIN mdl_course_categories category ON course.category = category.id

WHERE ( 1=1 ) 

GROUP BY course.fullname, course.id, course.visible, course.audiencevisible 

order by category_name

In reply to Saran Gokul

Re: Useful SQL Queries?

by MAG II -

Hi - 

I am attempting to implement your SQL structure into my Moodle instance. I want to be able to run reports of users who are enrolled in a course, completed a course, etc. I am using the plugin Configurable Reports to create the SQL report. I am currently getting an error (attached screenshot).


Do you have any suggestions of how I can get this to work? My apologies, but I am new to SQL. Any help would be so much appreciated. 

Attachment SQL attempt.PNG