What i have
Query:
SELECT u.firstname,u.lastname, u.institution as department, u.department as company, c.shortname,
to_char(to_timestamp(p.timecompleted)::date,'YYYY-MM-DD') AS completed
FROM mdl_course_completions AS p
JOIN mdl_course AS c ON p.course = c.id
JOIN mdl_user AS u ON p.userid = u.id
WHERE c.enablecompletion = 1 AND u.firstname is NOT NULL AND p.timecompleted is NOT NULL
ORDER BY u.firstname
Results:
firstname lastname department company course completed
u1 u1 x x c1 date
u1 u1 x x c2 date
What i need
I need to be able to transport course to columns. Resulting in something similar to this:
firstname lastname department company c1 c2
u1 u1 x x date date
u2 u2 x x date date
I have tried using crosstab, but I am not skilled enough on SQL. Could someone please help?
EDIT: the number of courses are in the hundreds, so it needs to be dynamic.
(Also: English is not my first language, so please excuse any unclarities).