–方法一:PIVOT(聚合函数(列) FOR 列 in (…) )AS P
select name,
MAX(a.语文) as ‘语文’,-- 这里是结果的列名
MAX(a.数学) as ‘数学’,
MAX(a.英语) as ‘英语’
from Score pivot(max(Scores) for subject in(语文,数学,英语)) as a --in(xxx,xxx,xxx)里面的列的顺序要跟上面的一致
group by a.name
–方法二:case when then (推荐使用)
select name,
max(case when subject=‘语文’ then Scores else 0 end) ‘语文’,
max(case when subject=‘数学’ then Scores else 0 end) ‘数学’,
max(case when subject=‘英语’ then Scores else 0 end) ‘英语’
from Score
group by name