Add new columns to report

Re: Add new columns to report

by David Saylor -
Number of replies: 0
This should show teacher name and up to 3 cohorts as columns.

SELECT u.firstname AS 'First' , u.lastname AS 'Last',
CONCAT(u.firstname,' ',u.lastname) AS 'Display Name',
CONCAT(tu.firstname,' ',tu.lastname) AS 'Teacher Name',
c.fullname AS 'Course',
cc.name AS 'Category',

CASE
WHEN gi.itemtype = 'course'
THEN c.fullname + ' Course Total'
ELSE gi.itemname
END AS 'Item Name',

ROUND(gg.finalgrade,2) AS Grade,
FROM_UNIXTIME(gg.timemodified) AS Time,
co1.name AS 'Cohort 1',
co2.name AS 'Cohort 2',
co3.name AS 'Cohort 3'

FROM prefix_course AS c
JOIN prefix_context AS ctx ON c.id = ctx.instanceid
JOIN prefix_role AS role ON role.shortname = 'student'
JOIN prefix_role_assignments AS ra ON ra.contextid = ctx.id AND ra.roleid = role.id
JOIN prefix_user AS u ON u.id = ra.userid
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

LEFT JOIN prefix_role AS trole ON trole.shortname LIKE '%teacher'
LEFT JOIN prefix_role_assignments AS tra ON tra.contextid = ctx.id AND tra.roleid = trole.id
LEFT JOIN prefix_user AS tu ON tu.id = tra.userid

LEFT JOIN prefix_cohort_members AS com1 ON com1.userid = u.id
LEFT JOIN prefix_cohort AS co1 ON co1.id = com1.cohortid
LEFT JOIN prefix_cohort_members AS com2 ON com2.userid = u.id AND com2.id > com1.id
LEFT JOIN prefix_cohort AS co2 ON co2.id = com2.cohortid
LEFT JOIN prefix_cohort_members AS com3 ON com3.userid = u.id AND com3.id > com2.id
LEFT JOIN prefix_cohort AS co3 ON co3.id = com3.cohortid
LEFT JOIN prefix_cohort_members AS com4 ON com4.userid = u.id AND com4.id < com3.id

WHERE gi.courseid = c.id
AND com4.id IS NULL
ORDER BY u.lastname
Average of ratings: Useful (1)