SQL的case when then else end语句的用法
- SELECT a.managecom,
- a.subtype,
- count(*) loadsucc,
- sum(case when a.state in ('4', '5', '6', '7', '8', '9') then 1 else 0 end) recogsucc,
- sum(case when a.state in ('3', '12', '13') then 1 else 0 end) recogfail,
- sum(case when a.state in ('1', '2') then 1 else 0 end) waitrecog
- FROM ocr_docdetail a, ocr_loaddetail c
- WHERE 1 = 1
- and a.managecom like '86%'
- and a.managecom = c.managecom
- and a.bussno = c.bussno
- and a.subtype = c.subtype
- and c.loadstate = 0
- and c.scandate >= date '2012-07-29'
- and c.scandate <= date '2013-01-07'
- group by a.managecom, a.subtype
- order by a.managecom, a.subtype;
case具有两种格式。简单case函数和case搜索函数。
--简单case函数
case sex
when '1' then '男'
when '2' then '女'
else '其他' end
--case搜索函数
case when sex = '1' then '男'
when sex = '2' then '女'
else '其他' end
这两种方式,可以实现相同的功能。简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判定式。
还有一个需要注重的问题,case函数只返回第一个符合条件的值,剩下的case部分将会被自动忽略。
--比如说,下面这段sql,你永远无法得到“第二类”这个结果
case when col_1 in ( 'a', 'b') then'第一类'
when col_1 in ('a') then '第二类'
else '其他' end
下面我们来看一下,使用case函数都能做些什么事情。
一,已知数据按照另外一种方式进行分组,分析。
有如下数据:(为了看得更清楚,我并没有使用国家代码,而是直接用国家名作为primary key)
|
国家(country) |
人口(population) |
|
中国 |
600 |
|
美国 |
100 |
|
加拿大 |
100 |
|
英国 |
200 |
|
法国 |
300 |
|
日本 |
250 |
|
德国 |
200 |
|
墨西哥 |
50 |
|
印度 |
250 |
根据这个国家人口数据,统计亚洲和北美洲的人口数量。应该得到下面这个结果。
|
洲 |
人口 |
|
亚洲 |
1100 |
|
北美洲 |
250 |
|
其他 |
700 |
想要解决这个问题,你会怎么做?生成一个带有洲code的view,是一个解决方法,但是这样很难动态的改变统计的方式。
假如使用case函数,sql代码如下:
select sum(population),
case country
when '中国' then'亚洲'
when '印度' then'亚洲'
when '日本' then'亚洲'
when '美国' then'北美洲'
when '加拿大' then'北美洲'
when '墨西哥' then'北美洲'
else '其他' end
from table_a
group by case country
when '中国' then'亚洲'
when '印度' then'亚洲'
when '日本' then'亚洲'
when '美国' then'北美洲'
when '加拿大' then'北美洲'
when '墨西哥' then'北美洲'
else '其他' end;
注:
在上面这个例子中,其实select 了两个字段:sum(population), case country;
country 字段里面原来有很多值,有“中国,美国,日本,加拿大...”等等,select后的case when then else end其实就是相当于把country的值分为三类:"亚洲,北美洲,其他";
其实没有from后面的case when语句,其结果集是这样的,sum得到的是人口population的总数,所以需要将population分组,就有了后面的group by:
后面的case when语句,其实是将前面查询的字段case country进行分组,分成三组:"亚洲,北美洲,其他",然后就能得出结果来
同样的,我们也可以用这个方法来判定工资的等级,并统计每一等级的人数。sql代码如下;
select
case when salary <= 500 then '1'
when salary > 500 and salary <= 600 then '2'
when salary > 600 and salary <= 800 then '3'
when salary > 800 and salary <= 1000 then '4'
else null end salary_class,
count(*)
from table_a
group by
case when salary <= 500 then '1'
when salary > 500 and salary <= 600 then '2'
when salary > 600 and salary <= 800 then '3'
when salary > 800 and salary <= 1000 then '4'
else null end;
二,用一个sql语句完成不同条件的分组。
有如下数据
|
国家(country) |
性别(sex) |
人口(population) |
|
中国 |
1 |
340 |
|
中国 |
2 |
260 |
|
美国 |
1 |
45 |
|
美国 |
2 |
55 |
|
加拿大 |
1 |
51 |
|
加拿大 |
2 |
49 |
|
英国 |
1 |
40 |
|
英国 |
2 |
60 |
按照国家和性别进行分组,得出结果如下
|
国家 |
男 |
女 |
|
中国 |
340 |
260 |
|
美国 |
45 |
55 |
|
加拿大 |
51 |
49 |
|
英国 |
40 |
60 |
普通情况下,用union也可以实现用一条语句进行查询。但是那样增加消耗(两个select部分),而且sql语句会比较长。
下面是一个是用case函数来完成这个功能的例子
select country,
sum( case when sex = '1' then
population else 0 end), --男性人口
sum( case when sex = '2' then
population else 0 end) --女性人口
from table_a
group by country;
这样我们使用select,完成对二维表的输出形式,充分显示了case函数的强大。
三,在check中使用case函数。
在check中使用case函数在很多情况下都是非常不错的解决方法。可能有很多人根本就不用check,那么我建议你在看过下面的例子之后也尝试一下在sql中使用check。
下面我们来举个例子
公司a,这个公司有个规定,女职员的工资必须高于1000块。假如用check和case来表现的话,如下所示
constraint check_salary check
( case when sex = '2'
then case when salary > 1000
then 1 else 0 end
else 1 end = 1 )
假如单纯使用check,如下所示
constraint check_salary check
( sex = '2' and salary > 1000 )
女职员的条件倒是符合了,男职员就无法输入了。
转自:http://blog.csdn.net/xuxurui007/article/details/8479953
SQL的case when then else end语句的用法的更多相关文章
- SQL利用Case When Then多条件判断SQL 语句
http://www.cnblogs.com/kevin2013/archive/2010/07/02/1769682.html SQL利用Case When Then多条件判断SQL ,用于sele ...
- sql学习. case + group by 都干了啥子事情
select case pref_name when 'fudao' then 'siguo' when 'xiangchuan' then 'siguo' when 'aiyuan' then 's ...
- 在论坛中出现的比较难的sql问题:15(生成动态删除列语句 分组内多行转为多列)
原文:在论坛中出现的比较难的sql问题:15(生成动态删除列语句 分组内多行转为多列) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路. 1.如果去掉这个临时表中合计为0 ...
- SQL语句---nvl 用法
SQL语句---nvl 用法 一NVL函数是一个空值转换函数 NVL(表达式1,表达式2) 如果表达式1为空值,NVL返回值为表达式2的值,否则返回表达式1的值. 该函数的目的是把一个空值(nul ...
- Sql Server 简单查询 异步服务器更新语句
//结构:select 子句 [into 子句] from 子句 [where 子句] [group by 子句] [having 子句] [order by 子句] select dept_c ...
- 在Hdsi2.0 SQL的注入部分抓包分析语句
在Hdsi2.0 SQL的注入部分抓包分析语句 恢复cmd ;insert tb1 exec master..xp_cmdshell''net user ''-- ;exec master.dbo.s ...
- SQL 语句日期用法及函数
SQL 语句日期用法及函数 --DAY().MONTH().YEAR()——返回指定日期的天数.月数.年数:select day(cl_s_time) as '日' from class --返回天 ...
- switch… case 语句的用法(一)
public class Test7 { public static void main(String[] args) { int i=5; switch(i) { case 1: System.ou ...
- switch… case 语句的用法
switch… case 语句的用法 public class Test7 { public static void main(String[] args) { int i=5; switch(i ...
随机推荐
- Java解析word,获取文档中图片位置
前言(背景介绍): Apache POI是Apache基金会下一个开源的项目,用来处理office系列的文档,能够创建和解析word.excel.ppt格式的文档. 其中对word文档的处理有两个技术 ...
- Logback分别打印info日志和error日志
<?xml version="1.0" encoding="utf-8" ?><configuration> <appender ...
- 跟我一起学JQuery插件开发教程
在逛codeproject网站的时候,突然看到一篇文章:How to write plugin in Jquery. 如果对E文好的同学 ,可以看上面的连接.现在我把上面网站的及结合自己的想法写这篇文 ...
- 用纯CSS实现的箭头
div+css实现带三角箭头提示框 链接:http://www.xuebuyuan.com/160534.html 链接:http://www.admin10000.com/document/4089 ...
- Python day 6(3) Python 函数式编程1
一:函数式编程概念 函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计.函数就是面向过程的 ...
- mysql目录迁移 更改mysql的存储目录
元旦节刚过完回来,忙了一天,现在的时间剩余不是很充足,所以更新简短的文章一篇! 正文: 正常情况下mysql的存储目录都是在/var/lib/mysql/下的,那么怎么将存储位置改到/data_mys ...
- 【STL深入理解】vector
这篇文章不打算讲述vector的基本用法,而是总结一下近期我大量阅读C++经典书籍时遇到的一些关于vector的容易忽略的知识点,特意将它们记录下来,以便以后查阅. 1.v[0]和v.at(0)的区别 ...
- asp.net mvc 客户端验证
插件 jQuery unobtrusive Validation @Html.TextBoxFor(x=>x.UserName) [StringLength(7,MinimumLength=2, ...
- Chris Richardson微服务翻译:构建微服务之微服务架构的进程通讯
Chris Richardson 微服务系列翻译全7篇链接: 微服务介绍 构建微服务之使用API网关 构建微服务之微服务架构的进程通讯(本文) 微服务架构中的服务发现 微服务之事件驱动的数据管理 微服 ...
- JSP执行过程分析
概述 在java领域,表现层技术主要有三种:jsp.freemarker.velocity.jsp是由sun公司倡导的官方标准,freemarker和velocity是第三方的模板. jsp是大家最熟 ...