This is the first time – I’m trying to do something like this – so please bear with me.
This is on MySql.
I am trying to generate a report to see which students have completed which topics and on which dates.
This is the current query that I run
select u.email,t.topic_name,tu.date_created as 'date completed' from topic_user tu join topic t ON tu.topic_id = t.topic_id join user u ON tu.user_id = u.user_id
which will return results like
email | topic_name | date completed abc@gmail.com | ABC | 03/01/2012 abc@gmail.com | DEF | 03/02/2012 abc@gmail.com | ABC | 03/08/2012 abc@gmail.com | GHI | 03/08/2012 def@gmail.com | ABC | 03/02/2012 def@gmail.com | XYZ | 03/10/2012
The way I want to generate the report is have the topic names as column headers and the date they completed it as values
email | ABC | DEF | GHI | JKL | XYZ abc@gmail.com | 03/08/2012 | 03/02/2012 | 03/08/2012 | null | null def@gmail.com | 03/02/2012 | null | null | null | 03/10/2012
Few things to note are:
1) All the topic names would come from the topic table – even if they have not been completed by the students – the value should appear as null
2) Incase of student abc@gmail.com – he has studied the topic ABC twice – but the report should get the latest date.
I guess I have to write a stored procedure to accomplish this. Like maybe first pull all the topic names from the topic table and then create a temp view and populate it.
I would appreciate any help you can provide.
Thanks much
I’ve not tested this, and my experience with MySQL is limited but I hope the below is what you are after. It dynamically creates the
SELECTstatement using theGROUP_CONCATfunction, then executes it (This is the bit I am not certain of the way to do it in MySQL).Of course if your topics are not changing very regularly you could just use:
and alter the query each time a new topic is added (This is the query produced by the process above).