Oracle CASE WHEN
时间: 2023-12-05 12:51:19 浏览: 96
Oracle_case_when_用法
Oracle CASE WHEN是一种条件表达式,用于在Oracle SQL查询中根据条件返回不同的结果。它的语法格式如下:
```
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN condition3 THEN result3
...
ELSE resultN
END
```
其中,condition1, condition2, condition3等为条件表达式,result1, result2, result3等为对应的结果,ELSE子句为可选项,表示当所有条件都不满足时的默认结果。
例如,以下查询使用CASE WHEN将销售额按照不同的区间进行分组:
```
SELECT
CASE
WHEN sales <= 1000 THEN '0-1000'
WHEN sales <= 5000 THEN '1000-5000'
WHEN sales <= 10000 THEN '5000-10000'
ELSE '10000+'
END AS sales_range,
COUNT(*) AS sales_count
FROM
sales_table
GROUP BY
CASE
WHEN sales <= 1000 THEN '0-1000'
WHEN sales <= 5000 THEN '1000-5000'
WHEN sales <= 10000 THEN '5000-10000'
ELSE '10000+'
END;
```
阅读全文