sql通用行列转换


--
行转列 select 姓名, SUM(case 课程 when '语文' then 分数 else 0 end) as 语文, SUM(case 课程 when '数学' then 分数 else 0 end) as 数学, SUM(case 课程 when '物理' then 分数 else 0 end) as 物理 from scores group by 姓名
--列转行 select 姓名,'语文' as 课程, 语文 as 分数 from scores2 union all select 姓名, '数学' as 课程,数学 as 分数from scores2 union all select 姓名,'物理' as 课程,物理 as 分数from scores2

猜你喜欢

转载自www.cnblogs.com/kerwincui/p/9139991.html
今日推荐