微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlserver 中case when用法小结

首先建表和插入数据语句:

use Student

go

create table score

(

学号 nvarchar(10),

课程 nvarchar(10),

成绩 int

)

go

insert into score values('0001','语文',87);

insert into score values('0001','数学',79);

insert into score values('0001','英语',95);

insert into score values('0002',69);

insert into score values('0002',84);

insert into score values('0001',95);

case when 用法一:

CASE 简单表达式,它通过将表达式与一组简单的表达式进行比较来确定结果。

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 score 

group by 学号

case when 用法二:

CASE 搜索表达式,它通过计算一组布尔表达式来确定结果。

select 学号,成绩,

case 成绩

when 87 then '良'

when 79 then '良'

when 95 then '优'

when 69 then '中'

 else '差' end as test

from score

 上面为本人对case when的理解,如有错误希望批评指出,多谢!

注:每个case 对应一列数据

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐