db2 case when
时间: 2023-09-07 17:17:50 浏览: 146
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.
阅读全文