zoukankan      html  css  js  c++  java
  • [Oracle/Sql] Decode与Case

    Decode和case都可以实现SQL中的条件结构,下面为用法示例:

    select id,name,score,decode(floor(score/20),5,'A',4,'B',3,'C',2,'D','E') as grade from tb_score01 order by grade

    运行情况:

    SQL> select id,name,score,decode(floor(score/20),5,'A',4,'B',3,'C',2,'D','E') as grade from tb_score01 order by grade;
    
            ID NAME                      SCORE G
    ---------- -------------------- ---------- -
             3 VJJYYVKXKJKZADERKU           84 B
            11 AGUNCTGZFGDJJGLQTQMY         81 B
             2 YUVFJNTQAYJBTDYAQNIB         50 D
            10 XDJFPNUIP                    53 D
             9 ZFJKTW                       59 D
             1 KBKVMELHCIFEWFI              42 D
             5 DUEEXRBJJI                    5 E
             8 TEEGBSRVHASOTBHN             30 E
             7 UAMTSMFDE                     2 E
            13 YUNHCQSBVPQ                  27 E
            14 VIDHHKOAWOTQXRBSI            14 E
    
            ID NAME                      SCORE G
    ---------- -------------------- ---------- -
             4 KBJWREHVF                     8 E
            15 RMWEROFCFEVVFRU              14 E
             6 QLLJNGXVFR                    2 E
            12 WNMWJP                       30 E
    
    已选择15行。

    Case示例:

    select id,name,score,case floor(score/20) when 5 then 'A' when 4 then 'B' when 3 then 'C' when 2 then 'D' else 'E' end as grade from tb_score01 order by grade 

    执行情况:

    SQL> select id,name,score,case floor(score/20) when 5 then 'A' when 4 then 'B' when 3 then 'C' when 2 then 'D' else 'E' end as grade from tb_score01 order by grade ;
    
            ID NAME                      SCORE G
    ---------- -------------------- ---------- -
             3 VJJYYVKXKJKZADERKU           84 B
            11 AGUNCTGZFGDJJGLQTQMY         81 B
             2 YUVFJNTQAYJBTDYAQNIB         50 D
            10 XDJFPNUIP                    53 D
             9 ZFJKTW                       59 D
             1 KBKVMELHCIFEWFI              42 D
             5 DUEEXRBJJI                    5 E
             8 TEEGBSRVHASOTBHN             30 E
             7 UAMTSMFDE                     2 E
            13 YUNHCQSBVPQ                  27 E
            14 VIDHHKOAWOTQXRBSI            14 E
    
            ID NAME                      SCORE G
    ---------- -------------------- ---------- -
             4 KBJWREHVF                     8 E
            15 RMWEROFCFEVVFRU              14 E
             6 QLLJNGXVFR                    2 E
            12 WNMWJP                       30 E
    
    已选择15行。

    表及充值语句如下:

    CREATE TABLE tb_score01
    (
        id NUMBER not null primary key,
        name varchar2(20),
        score int
    )
    
    Insert into tb_score01
    select rownum,dbms_random.string('*',dbms_random.value(6,20)),dbms_random.value(0,100) from dual
    connect by level<=15
    order by dbms_random.random

    2020-01-27

  • 相关阅读:
    常见的几种性能测试指标及计算公式
    性能测试分析
    性能测试的流程
    性能测试的基本知识
    Python的深拷贝、浅拷贝
    Http基础知识
    求List<int>中相等且连续的偶数的索引
    unity资源打包之后配置的生成
    unity在资源打包的时候,关于 hash 计算的简单总结
    C#中string.Join的用法
  • 原文地址:https://www.cnblogs.com/heyang78/p/12236138.html
Copyright © 2011-2022 走看看