db2中case when语句的用法 
时间: 2023-06-05 11:47:18 浏览: 575
在DB2中,CASE WHEN语句用于根据条件执行不同的操作。它的基本语法如下:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE default_result
END
其中,condition1、condition2等是条件表达式,result1、result2等是对应条件成立时的结果,default_result是当所有条件都不成立时的默认结果。
例如,以下语句将根据员工的工资水平返回不同的等级:
SELECT
CASE
WHEN salary >= 10000 THEN '高级'
WHEN salary >= 8000 THEN '中级'
WHEN salary >= 5000 THEN '初级'
ELSE '普通'
END AS level
FROM employee;
这将返回一个名为“level”的列,其中包含每个员工的工资等级。
相关问题
db2 case when
DB2 CASE WHEN is a conditional statement that allows you to perform different actions based on different conditions.
The syntax for DB2 CASE WHEN is as follows:
```
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE default_result
END
```
In this statement, you can specify one or more conditions, and their corresponding results. If none of the conditions are met, the default result is returned.
Here's an example of using DB2 CASE WHEN to assign a grade based on a student's score:
```
SELECT student_name,
score,
CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 80 THEN 'B'
WHEN score >= 70 THEN 'C'
WHEN score >= 60 THEN 'D'
ELSE 'F'
END AS grade
FROM student_scores;
```
In this example, if the student's score is greater than or equal to 90, they will receive an 'A' grade. If their score is between 80 and 89, they will receive a 'B' grade, and so on. If their score is below 60, they will receive an 'F' grade.
The result of this query will include the student's name, score, and grade.
db2 case when 多个条件
在DB2中,可以使用CASE WHEN语句来处理多个条件。CASE WHEN语句的语法如下:
CASE WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE resultN
END
其中,condition1、condition2等是条件表达式,result1、result2等是对应条件为真时的结果。如果所有条件都不满足,则返回ELSE后面的结果。
例如,如果我们想根据员工的薪水将其分类为低、中、高三个级别,可以使用以下SQL语句:
SELECT empno, sex, salary,
CASE WHEN salary < 20000 THEN 'low'
WHEN salary >= 20000 AND salary < 50000 THEN 'middle'
ELSE 'high'
END AS salaryclass
FROM employee
这个例子中,根据薪水的不同条件,使用CASE WHEN语句将其分类为不同的级别,并将结果命名为salaryclass。
#### 引用[.reference_title]
- *1* *2* *3* [DB2 使用Case语句](https://blog.csdn.net/Tlife/article/details/9173059)[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^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐













