DELETING DUPLICATES in Configurable Report

DELETING DUPLICATES in Configurable Report

by Renata Lasota -
Number of replies: 2

Dear Autors of this plugin and Moodle Users,

I've developed a report (Moodle 3.6) showing the following data:

user, e-mail, name, surname, course, category of course, type of activity, score, maximum score, %, if passed/failed, date when completed.

I've managed to create someting which looks like this (see below), but there is 1 issue: ROWS DUPLICATE (attachment), and with having many students attending many courses at the same time, this becomes really problematic. Unfortunately, this is where my knowledge of SQL ends, and my first thought was to turn to more experienced Users smile I hope this is the right place. Has anyone come accross a similar issue? I would be very gratefull for any guidance and help.

SELECT
u.username AS 'User',
u.email AS 'Email',
u.firstname AS 'Name' ,
u.lastname AS 'Surname',
c.shortname AS 'Course',
m.name AS 'ActivityType',
cc.name AS 'Category',
ROUND(gg.finalgrade,2) AS Score,ROUND(gg.rawgrademax,2) AS MAX, ROUND(gg.finalgrade / gg.rawgrademax * 100 ,2) AS Percentage,
 
IF (ROUND(gg.finalgrade / gg.rawgrademax * 100 ,2) > 75,'Yes' , 'No') AS Pass,
 
# cm.section AS Coursesection,

CASE
   WHEN cmc.completionstate = 0 THEN 'In Progress'
   WHEN cmc.completionstate = 1 THEN 'Completed'
   WHEN cmc.completionstate = 2 THEN 'Passed'
   WHEN cmc.completionstate = 3 THEN 'Failedl'
   ELSE 'Unknown'
END AS 'Progress',
DATE_FORMAT(FROM_UNIXTIME(cmc.timemodified), '%Y-%m-%d %H:%i') AS 'Date'

FROM prefix_course_modules_completion cmc
JOIN prefix_user u ON cmc.userid = u.id
JOIN prefix_course_modules cm ON cmc.coursemoduleid = cm.id
JOIN prefix_course c ON cm.course = c.id
JOIN prefix_modules m ON cm.module = m.id
JOIN prefix_context AS ctx ON c.id = ctx.instanceid
JOIN prefix_role_assignments AS ra ON ra.contextid = ctx.id
JOIN prefix_grade_grades AS gg ON gg.userid = u.id
JOIN prefix_grade_items AS gi ON gi.id = gg.itemid
JOIN prefix_course_categories AS cc ON cc.id = c.category
WHERE  gi.courseid = c.id AND gi.itemname != 'Attendance' and u.id > 2
%%FILTER_USERS:u.username%%
%%FILTER_SEARCHTEXT:m.name:~%%
%%FILTER_STARTTIME:cmc.timemodified:>%% %%FILTER_ENDTIME:cmc.timemodified:<%%
 
ORDER BY u.lastname


Thank you and have a nice day smile

Renata

Attachment Configurable report.png
Average of ratings: -
In reply to Renata Lasota

Re: DELETING DUPLICATES in Configurable Report

by Luis de Vasconcelos -
It seems like you have lots of rows for each student, so have a look at the SQL PIVOT function.
https://modern-sql.com/use-case/pivot

How many Coursesection's do you have?

Lastly, have a good look at the execution plan for that query. On my Moodle database the query ran for a very long time and returned 17 million rows when I forced it to stop!
In reply to Luis de Vasconcelos

Odp: Re: DELETING DUPLICATES in Configurable Report

by Renata Lasota -

Hello,

Thank you very much for your response! There are usually around 4-5 coursesections. I think I need a little more SQL training then  shy and perhaps more help from my IT team shy . If they have time, I will surely tell them your suggestions. Thank you once more and have a nice day!