ORACLE case when then

Oracle CASE WHEN 用法介绍

1. CASE WHEN 表达式有两种形式

ORACLE  case when then
--简单Case函数  

CASE sex  
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE '其他' END --Case搜索函数 CASE
WHEN sex = '1' THEN '男'
WHEN sex = '2' THEN '女'
ELSE '其他' END
ORACLE  case when then

2. CASE WHEN 在语句中不同位置的用法

2.1 SELECT CASE WHEN 用法

ORACLE  case when then
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;
ORACLE  case when then

2.3 WHERE CASE WHEN 用法

ORACLE  case when then
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
ORACLE  case when then

2.4 GROUP BY CASE WHEN 用法

ORACLE  case when then
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 NULL END salary_class, -- 别名命名
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 NULL END;
ORACLE  case when then
上一篇:xcode插件XAlign


下一篇:【Python】python中的装饰器——@