select
t.'Display Name',
t.'Course',
t.'Category',
t.'Item Name',
MAX(CASE WHEN t.'idnumber' = 'a1' THEN t.'Grade' ELSE NULL END) AS 'оценка А',
MAX(CASE WHEN t.'idnumber' = 'b1' THEN t.'Grade' ELSE NULL END) AS 'оценка Б'
// и так далее: c1,d1 и t1
from
(
// ВАШ ЗАПРОС, добавил - gg.idnumber AS 'idnumber'
SELECT u.firstname AS 'First' , u.lastname AS 'Last', u.firstname + ' ' + u.lastname AS 'Display 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,
gg.idnumber AS 'idnumber'
FROM prefix_course AS c
JOIN prefix_context AS ctx ON c.id = ctx.instanceid
JOIN prefix_role_assignments AS ra ON ra.contextid = ctx.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
WHERE gi.courseid = c.id
ORDER BY lastname
) t
group by t.'Display Name',
t.'Course',
t.'Category',
t.'Item Name'