This question is different from what have been searching on in that it selects from more than one column before creating the pivot table I have a table structure similar to this
CREATE TABLE score (
id int,
reg_id varchar(5),
test1 int(5),
exam int(5),
subject int(5),
term VARCHAR(5)
)
CREATE TABLE subject (
id int,
subject_name varchar(5)
)
I want to be able to select more than one value e.g test1 and exam into different column in the DYNAMIC MYSQL PIVOT TABLE I have seen similar example like MySQL pivot row into dynamic number of columns , MySQL pivot table query with dynamic columns but they seems to be selecting only 1 matching value into the dynamic pivot table. I have tried writing something like this
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(case when subject = ''',
subject,
''' then test1 end)AS `',
(select subject_name from subject where id=subject), '`',
'SUM(case when subject = ''',
subject,
''' then exam end)AS `',
(select subject_name from subject where id=subject), '`'
)
) INTO @sql
FROM
scores;
SET @sql = CONCAT('SELECT reg_id, ', @sql, '
FROM scores where term="First term"
GROUP BY reg_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
but it is not working thank you
I want an output similar to this after the tranpose.
regid-------maths test----maths exam-----english test------english exam
3m --------- 10 ----- 23 ---- 12 ------- 59