mssql

推荐列表 站点导航

当前位置:首页 > 数据库 > mssql >

SQL集合函数中case when then 使用技巧

来源:互联网  作者:网络  发布时间:2020-12-07 11:41
我们都知道SQL中适用case when then来转化数据库中的信息 比如 select (case sex when 0 then...

那么在集合函数中它有什么用呢 ?

假设数据库有一张表名为student的表。

SQL集合函数中case when then 使用技巧

如果现在要你根据这张表,查出江西省男女个数,广东省男生个数,浙江省男女个数 怎么写SQL语句?即要生成下结果表

SQL集合函数中case when then 使用技巧

答案是:select sex ,count ( case province when '广东省' then '广东省' end )as 广东省 ,count ( case province when '江西省' then '江西省' end )as 江西省 ,count ( case province when '浙江省' then '浙江省' end )as 浙江省 from student group by sex

count()函数即根据给定的范围和group by(统计方式) 而统计行数据的条数

我们一步步来理解上面语句

1.  select sex from student (查询数据表中的存在的男女条数)

SQL集合函数中case when then 使用技巧

2.select sex, count (*) as num from student group by sex  (查询表中男女数量)

SQL集合函数中case when then 使用技巧

3.select sex ,province, count (*)as num from student group by sex,province (查询各省男女数量)

SQL集合函数中case when then 使用技巧

重点来了,如果我把count(*) 中的 *号换成任一列名呢? 如count(province) 会怎样?

4.select sex ,province, count (province)as num from student group by sex,province (查询各省男女数量)

结果跟上图一样:这说明换不换都一样。又有count (province)等价于 count(case province when '浙江省' then '浙江省' else province end )

但是如果我们缩小范围呢即count(case province when '浙江省' then '浙江省' end ) 那么请看下面

5.select sex ,province, count ( case province when '浙江省' then '浙江省' end )as num from student group by sex,province

SQL集合函数中case when then 使用技巧

即统计男女数量范围限定在浙江省 再精简一下即下面

6.select sex, count ( case province when '浙江省' then '浙江省' end ) as 浙江省 from student group by sex

SQL集合函数中case when then 使用技巧

已经接近我们的要求了,现在只要加上另几个字段就是了

7.select sex ,count ( case province when '广东省' then '广东省' end )as 广东省 ,count ( case province when '江西省' then '江西省' end )as 江西省 ,count ( case province when '浙江省' then '浙江省' end )as 浙江省 from student group by sex

SQL集合函数中case when then 使用技巧

小结:当然实现有很多种方法 可以多个子查询拼接起来也不无可厚非。我这只是一种思路

补充:case when then 知识点

(1) select (case province when '浙江省' then '浙江' when '江西省' then '江西' end  ) as 省份 from student

SQL集合函数中case when then 使用技巧

如果默认范围如果没全包含则为空 像上图的广东省为空

(2)select (case province when '浙江省' then '浙江' when '江西省' then '江西' else province end  ) as 省份 from student

SQL集合函数中case when then 使用技巧

相关热词: 技巧

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://www.juheyunku.com/sql/mssql/1011.shtml

最新文章
sql server 关于设置null的一 sql server 关于设置null的一

时间:2020-12-28

详解SQL游标的用法 详解SQL游标的用法

时间:2020-12-27

vs code连接sql server数据库步 vs code连接sql server数据库步

时间:2020-12-27

图书管理系统的sqlserver数 图书管理系统的sqlserver数

时间:2020-12-25

详解SQL 通配符 详解SQL 通配符

时间:2020-12-25

sql四大排名函数之ROW_NUM sql四大排名函数之ROW_NUM

时间:2020-12-25

SQLServer数据库处于恢复挂 SQLServer数据库处于恢复挂

时间:2020-12-24

Win10 64位安装个人版SQL20 Win10 64位安装个人版SQL20

时间:2020-12-24

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

SQL集合函数中case when then 使用技巧

2020-12-07 编辑:网络

那么在集合函数中它有什么用呢 ?

假设数据库有一张表名为student的表。

SQL集合函数中case when then 使用技巧

如果现在要你根据这张表,查出江西省男女个数,广东省男生个数,浙江省男女个数 怎么写SQL语句?即要生成下结果表

SQL集合函数中case when then 使用技巧

答案是:select sex ,count ( case province when '广东省' then '广东省' end )as 广东省 ,count ( case province when '江西省' then '江西省' end )as 江西省 ,count ( case province when '浙江省' then '浙江省' end )as 浙江省 from student group by sex

count()函数即根据给定的范围和group by(统计方式) 而统计行数据的条数

我们一步步来理解上面语句

1.  select sex from student (查询数据表中的存在的男女条数)

SQL集合函数中case when then 使用技巧

2.select sex, count (*) as num from student group by sex  (查询表中男女数量)

SQL集合函数中case when then 使用技巧

3.select sex ,province, count (*)as num from student group by sex,province (查询各省男女数量)

SQL集合函数中case when then 使用技巧

重点来了,如果我把count(*) 中的 *号换成任一列名呢? 如count(province) 会怎样?

4.select sex ,province, count (province)as num from student group by sex,province (查询各省男女数量)

结果跟上图一样:这说明换不换都一样。又有count (province)等价于 count(case province when '浙江省' then '浙江省' else province end )

但是如果我们缩小范围呢即count(case province when '浙江省' then '浙江省' end ) 那么请看下面

5.select sex ,province, count ( case province when '浙江省' then '浙江省' end )as num from student group by sex,province

SQL集合函数中case when then 使用技巧

即统计男女数量范围限定在浙江省 再精简一下即下面

6.select sex, count ( case province when '浙江省' then '浙江省' end ) as 浙江省 from student group by sex

SQL集合函数中case when then 使用技巧

已经接近我们的要求了,现在只要加上另几个字段就是了

7.select sex ,count ( case province when '广东省' then '广东省' end )as 广东省 ,count ( case province when '江西省' then '江西省' end )as 江西省 ,count ( case province when '浙江省' then '浙江省' end )as 浙江省 from student group by sex

SQL集合函数中case when then 使用技巧

小结:当然实现有很多种方法 可以多个子查询拼接起来也不无可厚非。我这只是一种思路

补充:case when then 知识点

(1) select (case province when '浙江省' then '浙江' when '江西省' then '江西' end  ) as 省份 from student

SQL集合函数中case when then 使用技巧

如果默认范围如果没全包含则为空 像上图的广东省为空

(2)select (case province when '浙江省' then '浙江' when '江西省' then '江西' else province end  ) as 省份 from student

SQL集合函数中case when then 使用技巧

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://www.juheyunku.com/sql/mssql/1011.shtml

相关文章

风云图片

推荐阅读

返回mssql频道首页