I want to generate a cross tab query in MySQL. I used group_concat
, but it is not working. I issued the following query to generate the year list:
set @v1 = (SELECT GROUP_CONCAT(DISTINCT
CONCAT('\\nsum(CASE WHEN myear=\"', myear ,'\"
THEN amount ELSE NULL END) AS\"', myear,'\"')) AS column_list
FROM mdata
where myear > 1972 and myear < 1974);
select myear, amount, @v1 from mdata;
It produced the list of years in @v1
but with binary field. I want to use it in my next query to make the cross tab query.
I can run the first query with PHP and store the value in a variable and use it in the next query, but how it can be done in MySQL?