我们都知道SQL中适用case when then来转化数据库中的信息

比如  select (case sex when 0 then '男' else '女' end) AS sex  from studentInfo

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

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

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

答案是:

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 (查询数据表中的存在的男女条数)

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

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

  重点来了,如果我把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

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

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

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

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

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

  补充:case when then 知识点

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

  如果默认范围如果没全包含则为空

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

Eg:查找每个订单中的报告情况

select id,ORDER_ID,
count(case STATUS when 'F' then true end ) as '已发送报告' ,
count(case STATUS when 'D' then true end ) as '已生成报告',
count(case STATUS when 'T' then true end ) as '未生成报告',
max(SERVICE_CNT) as '共计服务次数'
from APP_NUTRITION_SERVICE_DETAIL where DELETE_MARK='N' and ORDER_ID in(2016042501,2016042502) group by ORDER_ID
结果:array
(
[0] => Array
(
[id] => 6
[ORDER_ID] => 2016042501
[已发送报告] => 4
[已生成报告] => 1
[未生成报告] => 0
[共计服务次数] => 5
)
[1] => Array
(
[id] => 8
[ORDER_ID] => 2016042502
[已发送报告] => 1
[已生成报告] => 1
[未生成报告] => 0
[共计服务次数] => 2
)

 

 

selectid,ORDER_ID,count(caseSTATUSwhen'F'thentrueend)as'已发送报告',count(caseSTATUSwhen'D'thentrueend)as'已生成报告',count(caseSTATUSwhen'T'thentrueend)as'未生成报告',max(SERVICE_CNT)as'共计服务次数'fromAPP_NUTRITION_SERVICE_DETAILwhereDELETE_MARK='N'andORDER_IDin(2016042501,2016042502)groupbyORDER_ID

最新文章

  1. 05. Web大前端时代之:HTML5+CSS3入门系列~H5 多媒体系
  2. inux如何查看当前占用CPU或内存最多的进程
  3. 出现( linker command failed with exit code 1)错误总结 (转)
  4. div显示提示信息
  5. [USACO08DEC] Trick or Treat on the Farm
  6. Linux下常用程序的代理服务器(proxy)配置
  7. cocos2d 多点触控
  8. css改变背景透明度【转】
  9. 10段实用的HTML5代码
  10. 关于css float 属性以及position:absolute 的区别。
  11. Lucene自定义扩展QueryParser
  12. 创建DataTable并把列默认值
  13. [Mac] 使用Mac时的一些技巧
  14. VS2008发布程序
  15. LeetCodeOJ. Maximum Depth of Binary Tree
  16. T4模板生成不同部署环境下的配置文件
  17. netsh命令之ip设置
  18. cookie设置
  19. Useful Articles for the Oracle E-Business Suite Technology Stack for Technical Consultant / DBA [ID
  20. Python全栈-magedu-2018-笔记1

热门文章

  1. apache开源项目--lucence
  2. Rose
  3. [转]NHibernate之旅(13):初探立即加载机制
  4. bootstrap布局两列或者多列表单
  5. 通过DeveloperApi获取spark程序执行进度及异常
  6. 一分钟快速入门openstack
  7. sql server 分布式事务
  8. 微信开发第7章 通过accesstoken获取用户黑名单列表
  9. iOS开发 落地消息多的处理办法(仅供参考)
  10. Bootstrap-风格的下拉按框:Bootstrap Select