case when then 随手练_1

Posted cdpj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了case when then 随手练_1相关的知识,希望对你有一定的参考价值。

CASE WHEN THEN随手练,就当做练习指法吧

--drop table tbStudent
GO
Create table tbStudent(
    studentId int identity(1,1),
    fSex varchar(12),
    fProvince varchar(32)
)
GO
INSERT INTO tbStudent(fsex,fProvince)
               values(,江西省),
                     (,广东省),
                     (,浙江省),
                     (,江西省),
                     (,浙江省),
                     (,浙江省)
select *
from tbStudent
---------------------------------------------------------------------
--要查出:江西、广东、浙江 男女个数
select fSex,
          count(case when fProvince=江西省 then 江西省 end) as 江西省,
          count(case when fProvince=广东省 then 江西省 end) as 广东省,
          count(case when fProvince=浙江省 then 江西省 end) as 浙江省
from tbStudent
group by fSex
---------------------------------------------------------------------
--要查出:江西、广东、浙江 男女个数
select fSex,
          count(case fProvince when 江西省 then 江西省 end) as 江西省,
          count(case fProvince when 广东省 then 江西省 end) as 广东省,
          count(case fProvince when 浙江省 then 江西省 end) as 浙江省
from tbStudent
group by fSex
---------------------------------------------------------------------

---------------------------------------------------------------------

 运行结果:select * from tbStudent                        CASE WEHN THEN之后

          技术图片                                         技术图片

 

以上是关于case when then 随手练_1的主要内容,如果未能解决你的问题,请参考以下文章

函数随手练_1

函数随手练_1

case when 使用

spark-sql case when 问题

随手练——拼接最小字典序

MYSQL_批量更新