首頁  >  文章  >  資料庫  >  oracle case when的用法

oracle case when的用法

王林
王林原創
2019-10-24 09:33:5013697瀏覽

oracle case when的用法

CASE WHEN 在語句中不同位置的用法

#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 、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

3、GROUP BY CASE WHEN 用法

SELECT  
CASE WHEN salary <= 500 THEN &#39;1&#39;  
WHEN salary > 500 AND salary <= 600  THEN &#39;2&#39;  
WHEN salary > 600 AND salary <= 800  THEN &#39;3&#39;  
WHEN salary > 800 AND salary <= 1000 THEN &#39;4&#39;  
ELSE NULL END salary_class, -- 别名命名
COUNT(*)  
FROM    Table_A  
GROUP BY  
CASE WHEN salary <= 500 THEN &#39;1&#39;  
WHEN salary > 500 AND salary <= 600  THEN &#39;2&#39;  
WHEN salary > 600 AND salary <= 800  THEN &#39;3&#39;  
WHEN salary > 800 AND salary <= 1000 THEN &#39;4&#39;  
ELSE NULL END;

推薦教學:my教學教學

以上是oracle case when的用法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn