我们都知道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

case when then 与 count联合使用的更多相关文章

  1. group by 和count 联合使用问题

    工作中要根据用户发布的产品数量来排序做分页,使用group by uid 用count(uid) 来统计的数量和想要的数量不正确. count统计的数量是被group by 分组以后每一组中数据的数量 ...

  2. group by和count联合使用问题

    要根据用户发布的产品数量来排序做分页,使用group ) FROM( SELECT uid,COU 工作中要根据用户发布的产品数量来排序做分页,使用group by uid 用count(uid) 来 ...

  3. PostgreSQL的查询技巧: 零除, GENERATED STORED, COUNT DISTINCT, JOIN和数组LIKE

    零除的处理 用NULLIF(col, 0)可以避免复杂的WHEN...CASE判断, 例如 ROUND(COUNT(view_50.amount_in)::NUMERIC / NULLIF(COUNT ...

  4. count(*) vs count(1)--social.msdn.microsoft.com

    Clever response Dave, but insufficient. I'll admit I've suggested this myself for certain questions ...

  5. mysql 聚集函数 count 使用详解(转载)

    本文将探讨以下问题 1.count(*) . count(n).count(null)与count(fieldName)2.distinct 与 count 连用3.group by (多个字段) 与 ...

  6. 数据分组、统计 case when then else end

    case when 对表进行条件分组 case简单函数 case   age  when   then select name , sex , age , ( case age /*when 条件成立 ...

  7. mysql 聚集函数 count 使用详解

    mysql 聚集函数 count 使用详解 本文将探讨以下问题 1.count(*) . count(n).count(null)与count(fieldName) 2.distinct 与 coun ...

  8. 华为C语言编程规范

    DKBA华为技术有限公司内部技术规范DKBA 2826-2011.5C语言编程规范2011年5月9日发布 2011年5月9日实施华为技术有限公司Huawei Technologies Co., Ltd ...

  9. nyoj 1022 合纵连横【并查集节点的删除】

    合纵连横 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法 ...

随机推荐

  1. [Hadoop源码解读](三)MapReduce篇之Job类

    下面,我们只涉及MapReduce 1,而不涉及YARN. 当我们在写MapReduce程序的时候,通常,在main函数里,我们会像下面这样做.建立一个Job对象,设置它的JobName,然后配置输入 ...

  2. 2016值得关注的语言平台、JS框架

    语言和平台 Python 3.5 在今年发布了,带来了很多新特性 比如 Asyncio,,为你带来了类似 node.js 的事件机制,还有type hints. 鉴于Python 3 终于真正地火起来 ...

  3. HTTP缓存是如何实现

    浏览器是如何知道使用缓存的,其实这都是通过http中,浏览器将最后修改时间发送请求给web服务器,web服务器收到请求后跟服务器上的文档最后修改的时间对比,如果web服务器上最新文档修改时间小于或者等 ...

  4. js正则表达式及代码

    //校验是否全由数字组成function isDigit(s){var patrn=/^[0-9]{1,20}$/;if (!patrn.exec(s)) return falsereturn tru ...

  5. 终极秘籍教你怎么找回被盗iPhone 查询ICCID

    iPhone不慎丢失后怎么办?普通青年:立刻报警,基本没用.文艺青年:用Find my iPhone查找位置.但那只是个大概位置,iPhone关机后更是没戏,接着是用iCloud锁定手机,发送警告信息 ...

  6. [BILL WEI]SQL 如何将查询到的列作为表名去查询数据

    我们在做sql查询的时候,有时候需要将查询的列作为表名,去引用,然后再次查询 declare @table_name varchar(20) select @table_name=table_name ...

  7. dev/null和dev/zero区别 以及换回设备(loopback device)

    转自:http://blog.chinaunix.net/uid-20729677-id-765105.html dev/zero,是一个输入设备,你可你用它来初始化文件. /dev/zero---- ...

  8. 洛谷P1220 关路灯

    洛谷1220 关路灯 题目描述 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏地关 ...

  9. localtime和localtime_r

    上程序: #include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h ...

  10. Esper系列(九)NamedWindow语法create、Insert、select

    功能:用于存储一种或多种类型的事件的集合,并能对所存储的事件进行增删改查操作. CreateNameWindow 根据已有的数据源构造 格式: 1  [context context_name]  2 ...