SELECT user_name , MAX(CASE course WHEN '数学' THEN score ELSE 0 END ) 数学, MAX(CASE course WHEN '语文' THEN score ELSE 0 END ) 语文, MAX(CASE course WHEN '英语' THEN score ELSE 0 END ) 英语 FROM test_tb_grade GROUP BY USER_NAME;
select user_name, '语文' COURSE , CN_SCORE as SCORE from test_tb_grade2 union select user_name, '数学' COURSE, MATH_SCORE as SCORE from test_tb_grade2 union select user_name, '英语' COURSE, EN_SCORE as SCORE from test_tb_grade2 order by user_name,COURSE;
行转列:
SELECT tt.dept_name AS NAME,
SUM(( CASE WHEN (issuccess=1 OR issuccess=0 ) THEN 1 ELSE 0 END)) value1,
SUM(( CASE WHEN issuccess=1 THEN 1 ELSE 0 END )) value2
FROM tr_cockpit_kyxx t,tr_department tt WHERE t.dept_id=tt.id AND nf='2018' GROUP BY dept_id ;