如数据库中存在下面一张二维表,需要将科目这一列转成行。
转换后的数据如下所示:
SELECT NAME,max( CASE WHEN SUBJECT = '语文' THEN score ELSE 0 END ) AS 语文,max( CASE WHEN SUBJECT = '数学' THEN score ELSE 0 END ) AS 数学FROMstudent_scoreGROUP BYNAME;
(select name,'语文' as subjet, chinese as score from exam_score)UNION(select name,'数学' as subjet, math as score from exam_score)
