码迷,mamicode.com
首页 > 数据库 > 详细

sql-case列转行

时间:2015-04-17 15:31:56      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

1:对列进行逻辑判断
select ID,Score,
case
 when Score>=90 then ‘A‘
 when Score>=80 then ‘B‘
 when Score>=70 then ‘C‘
 when Score>=60 then ‘D‘
 when Score>=50 then ‘E‘
  end
from dbo.Scores

  

 
2:列转行
如果列名车位,某某,那么分数列
select stuNo,
sum(case when Subject=‘生物‘ then Score end) as 生物,
sum(case when Subject=‘数学‘ then Score end) as 数学,
sum(case when Subject=‘英语‘ then Score end) as 英语,
sum(case when Subject=‘语文‘ then Score end) as 语文
  from dbo.Scores group by StuNo

  

 

sql-case列转行

标签:

原文地址:http://www.cnblogs.com/mongo/p/4434839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!