oracle case when 字段比较
时间: 2023-09-27 19:09:47 浏览: 127
Oracle 技巧
在Oracle中,可以使用CASE WHEN语句来比较字段的值。在使用CASE WHEN语句时,可以根据需要进行等值判断或表达式判断。与DECODE函数相比,CASE WHEN更灵活,可以用于=,>=,<,<=,<>,IS NULL,IS NOT NULL,BETWEEN等多种判断条件。以下是一些示例:
1. 使用CASE WHEN进行等值判断:
```sql
SELECT CASE WHEN 字段名 = '1' THEN '条件1'
WHEN 字段名 = '2' THEN '条件2'
ELSE '其他条件'
END AS 别名
FROM 表名;
```
2. 使用CASE WHEN进行表达式判断:
```sql
SELECT CASE WHEN INSTR(hiredate, '82') = 0 THEN 0
ELSE 1
END AS "year"
FROM emp;
```
3. 使用CASE WHEN进行多条件判断和求和:
```sql
SELECT COUNT(*) AS total,
SUM(CASE WHEN INSTR(hiredate, '80') = 0 THEN 0
ELSE 1
END) AS "1980",
SUM(CASE WHEN INSTR(hiredate, '81') = 0 THEN 0
ELSE 1
END) AS "1981",
SUM(CASE WHEN INSTR(hiredate, '82') = 0 THEN 0
ELSE 1
END) AS "1982",
SUM(CASE WHEN INSTR(hiredate, '87') = 0 THEN 0
ELSE 1
END) AS "1987"
FROM emp;
```
以上是使用CASE WHEN对字段进行比较的示例。可以根据实际需求灵活运用CASE WHEN来满足不同的条件判断和逻辑运算。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Oracle case when对字段的空值判断及其两种用法](https://blog.csdn.net/weixin_40364882/article/details/118394307)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [oracle 学习笔记 - DECODE函数和CASE WHEN 比较](https://blog.csdn.net/u010758410/article/details/77285290)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文