SQL之case when then用法

来源:互联网 发布:windows下执行sh脚本 编辑:程序博客网 时间:2024/05/26 20:24


--简单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  

下面实例演示:
首先创建一张users表,其中包含id,name,sex三个字段,表内容如下:

复制代码
SQL> drop table users purge; drop table users purge ORA-00942: 表或视图不存在SQL> create table users(id int,name varchar2(20),sex number); Table createdSQL> insert into users(id,name) values(1,'张一'); 1 row insertedSQL> insert into users(id,name,sex) values(2,'张二',1); 1 row insertedSQL> insert into users(id,name) values(3,'张三'); 1 row insertedSQL> insert into users(id,name) values(4,'张四'); 1 row insertedSQL> insert into users(id,name,sex) values(5,'张五',2); 1 row insertedSQL> insert into users(id,name,sex) values(6,'张六',1); 1 row insertedSQL> insert into users(id,name,sex) values(7,'张七',2); 1 row insertedSQL> insert into users(id,name,sex) values(8,'张八',1); 1 row insertedSQL> commit; Commit completeSQL> select * from users;                                      ID NAME                        SEX--------------------------------------- -------------------- ----------                                      1 张一                                                       2 张二                          1                                      3 张三                                                       4 张四                                                       5 张五                          2                                      6 张六                          1                                      7 张七                          2                                      8 张八                          1 8 rows selected
复制代码

1、上表结果中的"sex"是用代码表示的,希望将代码用中文表示。可在语句中使用case语句:

复制代码
SQL> select u.id,u.name,u.sex,  2    (case u.sex  3      when 1 then ''  4      when 2 then ''  5      else '空的'  6      end  7     )性别  8  from users u;                                      ID NAME                        SEX 性别--------------------------------------- -------------------- ---------- ------                                      1 张一                            空的                                      2 张二                          13 张三                            空的                                      4 张四                            空的                                      5 张五                          26 张六                          17 张七                          28 张八                          18 rows selected
复制代码

2、如果不希望列表中出现"sex"列,语句如下:

复制代码
SQL> select u.id,u.name,  2    (case u.sex  3      when 1 then ''  4      when 2 then ''  5      else '空的'  6      end  7     )性别  8  from users u;                                      ID NAME                 性别--------------------------------------- -------------------- ------                                      1 张一                 空的                                      2 张二                 男                                      3 张三                 空的                                      4 张四                 空的                                      5 张五                 女                                      6 张六                 男                                      7 张七                 女                                      8 张八                 男 8 rows selected
复制代码

3、将sum与case结合使用,可以实现分段统计。
     如果现在希望将上表中各种性别的人数进行统计,sql语句如下:

复制代码
SQL> select  2    sum(case u.sex when 1 then 1 else 0 end)男性,  3    sum(case u.sex when 2 then 1 else 0 end)女性,  4    sum(case when u.sex <>1 and u.sex<>2 then 1 else 0 end)性别为空  5  from users u;         男性         女性       性别为空---------- ---------- ----------         3          2          0--------------------------------------------------------------------------------SQL> select  2    count(case when u.sex=1 then 1 end)男性,  3    count(case when u.sex=2 then 1 end)女,  4    count(case when u.sex <>1 and u.sex<>2 then 1 end)性别为空  5  from users u;         男性          女       性别为空---------- ---------- ----------         3          2          0
--简单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  

下面实例演示:
首先创建一张users表,其中包含id,name,sex三个字段,表内容如下:

复制代码
SQL> drop table users purge; drop table users purge ORA-00942: 表或视图不存在SQL> create table users(id int,name varchar2(20),sex number); Table createdSQL> insert into users(id,name) values(1,'张一'); 1 row insertedSQL> insert into users(id,name,sex) values(2,'张二',1); 1 row insertedSQL> insert into users(id,name) values(3,'张三'); 1 row insertedSQL> insert into users(id,name) values(4,'张四'); 1 row insertedSQL> insert into users(id,name,sex) values(5,'张五',2); 1 row insertedSQL> insert into users(id,name,sex) values(6,'张六',1); 1 row insertedSQL> insert into users(id,name,sex) values(7,'张七',2); 1 row insertedSQL> insert into users(id,name,sex) values(8,'张八',1); 1 row insertedSQL> commit; Commit completeSQL> select * from users;                                      ID NAME                        SEX--------------------------------------- -------------------- ----------                                      1 张一                                                       2 张二                          1                                      3 张三                                                       4 张四                                                       5 张五                          2                                      6 张六                          1                                      7 张七                          2                                      8 张八                          1 8 rows selected
复制代码

1、上表结果中的"sex"是用代码表示的,希望将代码用中文表示。可在语句中使用case语句:

复制代码
SQL> select u.id,u.name,u.sex,  2    (case u.sex  3      when 1 then ''  4      when 2 then ''  5      else '空的'  6      end  7     )性别  8  from users u;                                      ID NAME                        SEX 性别--------------------------------------- -------------------- ---------- ------                                      1 张一                            空的                                      2 张二                          13 张三                            空的                                      4 张四                            空的                                      5 张五                          26 张六                          17 张七                          28 张八                          18 rows selected
复制代码

2、如果不希望列表中出现"sex"列,语句如下:

复制代码
SQL> select u.id,u.name,  2    (case u.sex  3      when 1 then ''  4      when 2 then ''  5      else '空的'  6      end  7     )性别  8  from users u;                                      ID NAME                 性别--------------------------------------- -------------------- ------                                      1 张一                 空的                                      2 张二                 男                                      3 张三                 空的                                      4 张四                 空的                                      5 张五                 女                                      6 张六                 男                                      7 张七                 女                                      8 张八                 男 8 rows selected
复制代码

3、将sum与case结合使用,可以实现分段统计。
     如果现在希望将上表中各种性别的人数进行统计,sql语句如下:

复制代码
SQL> select  2    sum(case u.sex when 1 then 1 else 0 end)男性,  3    sum(case u.sex when 2 then 1 else 0 end)女性,  4    sum(case when u.sex <>1 and u.sex<>2 then 1 else 0 end)性别为空  5  from users u;         男性         女性       性别为空---------- ---------- ----------         3          2          0--------------------------------------------------------------------------------SQL> select  2    count(case when u.sex=1 then 1 end)男性,  3    count(case when u.sex=2 then 1 end)女,  4    count(case when u.sex <>1 and u.sex<>2 then 1 end)性别为空  5  from users u;         男性          女       性别为空---------- ---------- ----------         3          2          0
0 0
原创粉丝点击