您好,欢迎访问一九零五行业门户网

Oracle CASE WHEN 用法介绍

1. case when 表达式有两种形式 --简单case函数 case sex when
1. case when 表达式有两种形式
--简单case函数 
case sex 
when '1' then '男' 
when '2' then '女' 
else '其他' end 
--case搜索函数 
case
when sex = '1' then '男' 
when sex = '2' then '女' 
else '其他' end
2. case when 在语句中不同位置的用法
2.1 select case when 用法
select  grade, count (case when sex = 1 then 1      /*sex 1为男生,2位女生*/
                      else null
                      end) 男生数,
                count (case when sex = 2 then 1
                      else null
                      end) 女生数
    from students group by grade;
2.2 where case when 用法
select t2.*, t1.*
  from t1, t2
  where (case when t2.compare_type = 'a' and
                  t1.some_type like 'nothing%'
                then 1
              when t2.compare_type != 'a' and
                  t1.some_type not like 'nothing%'
                then 1
              else 0
          end) = 1
2.3 group by case when 用法
select 
case when salary when salary > 500 and salary when salary > 600 and salary when salary > 800 and salary else null end salary_class, -- 别名命名
count(*) 
from    table_a 
group by 
case when salary when salary > 500 and salary when salary > 600 and salary when salary > 800 and salary else null end;
3.关于if-then-else的其他实现
3.1 decode() 函数
, , , , )
from  employees;
貌似只有oracle提供该函数,而且不支持ansi sql,语法上也没case when清晰,,个人不推荐使用。
3.2 在where中特殊实现t1.some_type )
        t1.some_type )
这种方法也是在特殊情况下使用,要多注意逻辑,不要弄错。
--------------------------------------------------------------------------------
linux-6-64下安装oracle 12c笔记
在centos 6.4下安装oracle 11gr2(x64)
oracle 11gr2 在vmware虚拟机中安装步骤
debian 下 安装 oracle 11g xe r2
--------------------------------------------------------------------------------
本文永久更新链接地址:
其它类似信息

推荐信息