Oracle-No.05 Oracle CASE 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 在语句中不同位置的用法

    1)select case when用法

select grade, count(case when sex = 1 then '男'
                    else null end) 男生数,
              count(case when sex = 2 then '女'
                    else null end) 女生数
        from students group by grade;

    2)group by case when用法

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 numm end salary_calss,
         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 numm end;

    3)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 2
            else 0 end
    ) = 1


转载于:https://my.oschina.net/shma1664/blog/316190

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值