标签:
以前面试老遇到一个行列转换的问题,今天没事,顺便记录一下
假设有这样一张表,如下图,创建表就不说了,直接建或者SQL语句都行
sql语句如下
--第一种 select name as 姓名, max(case Subject when ‘语文‘ then Result else 0 end) as 语文, max(case Subject when ‘数学‘ then result else 0 end) as 数学, max(case Subject when ‘英语‘ then Result else 0 end) as 英语, max(case Subject when ‘政治‘ then Result else 0 end) as 政治, max(case Subject when ‘物理‘ then Result else 0 end) as 物理 from cj group by Name --第二种 select Subject as 科目, MAX(case Name when ‘张三‘ then Result else 0 end) as 张三, MAX(case Name when ‘李四‘ then Result else 0 end) as 李四, MAX(case Name when ‘王五‘ then Result else 0 end) as 王五 from cj group by Subject
运行结果如下
标签:
原文地址:http://www.cnblogs.com/jkqnb/p/5812401.html