select 姓名 ,
max(case 课程 when '语文' then 分数 end ) 语文,
max(case 课程 when '数学' then 分数 end) 数学,
max(case 课程 when '物理' then 分数 end) 物理
from tb
group by 姓名select 姓名,
case when 课程='语文' then 分数 end 语文,
case when 课程='数学' then 分数 end 数学
from tb
/*这两种结果不一样。等解释*/
解决方案 »
- 必须声明标量变量 "@CourseID"。
- 关于 update 的问题
- 如何删除同一表中特定字符串!
- sqlserver中建表时这么让字段有自动递增的属性?
- PATINDEX('%[^-^.^0-9]%',col+'a')中^和那个.是什么意思
- 请教邹捷:用Log Explorer for SQL Server 恢复数据的问题
- 建立存储过程和Select 取值 。请问2种方法主要有什么区别啊。急。。。。。
- 求SQL语句:怎么用存储过程向表中插入成批的数据????
- 求救!高手进来看看,关于高效率抽取数据。。。
- to jcq(疯子弟),hailong525(风云)
- 数据库中怎么连续读取下一条记录?
- 关于case when then 的用法,能给我解释哈下面的代码吗?
用MAX 函数可以排除NULL 这样每组只对应一个列值
可以理解为select 姓名 ,
max(case when 课程='语文' then 分数 end ) 语文,
max(case when 课程='语文' then 分数 end) 数学,
max(case when 课程='语文' then 分数 end) 物理
from tb
group by 姓名
case when 课程='语文' then 分数 end 语文,
case when 课程='数学' then 分数 end 数学
from tb
呵呵,这样你可以看得很清楚了呀,
如果有几个是会分开在每行的,加个MAX就是取最大值呀
when age>=2 and age<=11 then '儿童'
when age>=12 and age<18 then '少年'
when age>=18 and age<=30 then '青年'
when age>30 and age<60 then '中年'
when age>=60 and age<=100 then '老年'
else '过百岁寿星'
from tb;
when age>=2 and age <=11 then '儿童'
when age>=12 and age <18 then '少年'
when age>=18 and age <=30 then '青年'
when age>30 and age <60 then '中年'
when age>=60 and age <=100 then '老年'
else '过百岁寿星' end) as '人生就那样'
from tb;