I have a sql query like this. How to make it dynamic?
SELECT name,
MAX(CASE WHEN sub = 'maths' then 'y' ELSE 'n' END) AS maths,
MAX(CASE WHEN sub = 'science' then 'y' ELSE 'n' END) AS science,
MAX(CASE WHEN sub = 'history' then 'y' ELSE 'n' END) AS history,
MAX(CASE WHEN sub = 'computer'then 'y' ELSE 'n' END) AS computer,
MAX(CASE WHEN sub = 'english' then 'y' ELSE 'n' END) AS english
FROM table t
GROUP BY name;
so that final result is like :
name maths science history computer english
a y y y y y
b y y y n n
c y n y y n
Also how to select y or n
as a column value? Will select work?