GaussDB SQL basic syntax example-CASE expression

Table of contents

I. Introduction

2. Introduction to CASE Expression

3. Simple CASE expression in GaussDB database

1. Basic concepts

2. Basic grammar

3. Example

4. Search CASE expression in GaussDB database

1. Basic concepts

2. Basic grammar

3. Example

5. Summary

I. Introduction

SQL is a standard computer language used to access and manipulate databases. GaussDB supports the SQL standard (main features of SQL2, SQL3 and SQL4 are supported by default).

This series will be introduced based on the online document "Cloud Database GaussDB—SQL Reference".

2.CASE Expression (CASE expression) introduction

In GaussDB SQL, CASE expression (CASE Expression) is a very powerful and commonly used tool that can be used to perform condition-based operations in SQL. CASE expressions are similar to IF-THEN-ELSE statements, but are more flexible to use and easier to read and write.

The CASE expression contains two forms, one is the simple form and the other is the search form. The writing, grammar and usage of these two forms will be introduced below.

3.Simple CASE expression in GaussDB database

1, Basic Concepts

A simple CASE expression performs an equality-based comparison on a given expression and performs an operation if the expression is equal to a certain value. That is, jump to the corresponding result_expression based on the matching result of input_expression and when_expression.

2, basic language

CASE input_expression

WHEN when_expression THEN result_expression

[...n]

[ELSE else_result_expression]

END

Explanation: Among them, input_expression represents the expression that needs to be compared, when_expression, etc. represents the value that needs to be compared, result_expression, etc. represents the result when each value is equal, else_result_expression represents when input_expression is not equal to Default result for any value

Tip:CASE: The simple CASE function supports subqueries, but please note that input_expression and when_expression can match. In addition, if there is no input_expression = when_expression that evaluates to TRUE, DLI will return else_result_expression when the ELSE clause is specified; when the ELSE clause is not specified, a NULL value will be returned.

3, example

Assuming that the employee level is defined based on a fixed salary, it can be executed according to the following SQL:

SELECT name

      ,salary

      ,CASE salary

                 WHEN 15000 THEN 'Elementary'

                       WHEN 20000 THEN 'Intermediate'

                            WHEN 25000 THEN 'Premium'

                            WHEN 30000 THEN 'Premium'

                            WHEN 35000 THEN 'Premium'

                            ELSE NULL

                   END AS level

FROM company

ORDER BY salary;

SQLLanguage analysis:

The test uses a company table created in the GaussDB database.

This SQL selects the employee's name (name), salary (salary), and a field classified according to salary level (level) from the "company" table. And use the "ORDER BY salary" statement to sort the results from low to high salary.

How simple CASE expressions work:

If the value of the "salary" field is equal to 15000, then the value of the "level" field is "junior".

If the value of the "salary" field is equal to 20000, then the value of the "level" field is "intermediate".

If the value of the "salary" field is equal to 25000 or 30000 or 35000, then the value of the "level" field is "advanced".

If the value of the "salary" field is not equal to any of the above values, then the value of the "level" field is NULL.

4.Search CASE expression in GaussDB database

1, Basic Concepts

Searching for a CASE expression means performing an inequality-based comparison on a given expression, and performing the corresponding operation if the expression satisfies the given condition. That is, the boolean_expression of each WHEN clause is evaluated in the specified order. Returns the result_expression of the first boolean_expression that evaluates to TRUE.

2, basic language

CASE WHEN boolean_expression THEN result_expression

[...n]

[ELSE else_result_expression]

END

Explanation: Among them, boolean_expression, etc. represent the conditions that need to be compared, result_expression, etc. represent the operation results when the corresponding conditions are met, else_result_expression represents the default result when expression does not meet any conditions.

Tip:boolean_expression: It can contain subqueries, but the return value of the entire boolean_expression expression can only be of Boolean type.

If there is no Boolean_expression that evaluates to TRUE, DLI will return else_result_expression when the ELSE clause is specified; when the ELSE clause is not specified, a NULL value will be returned.

3, example

Assuming that the employee level is defined based on the salary range, it can be executed according to the following SQL:

SELECT name

      ,salary

      ,CASE WHEN salary < 15000 THEN '初级'

                       WHEN salary BETWEEN 15000 AND 25000 THEN '中级'

                            WHEN salary >25000 THEN 'Senior'

                            ELSE NULL

                   END AS level

FROM company

ORDER BY salary;

SQLLanguage analysis:

This SQL is the same as above. It selects the employee's name (name), salary (salary) and classification according to the salary level (level) from the "company" table.

How search CASE expressions work:

When the salary (salary) is less than 15,000, the salary grade is marked as "junior".

When the salary is between 15,000 and 25,000 (inclusive), the salary grade is marked as "intermediate".

When the salary is greater than 25,000, the salary grade is marked as "senior".

If none of the above conditions are met, the salary grade is marked as NULL.

综上,这个SQL语句的主要目的是获取员工信息,根据其薪水水平对其进行分类,并按照薪水的水平进行排序。

五、小结

GaussDB 中的CASE表达式是一个非常有用的工具,可以用于在SQL中执行基于条件的操作,实现条件判断和分支逻辑,进一步优化数据库查询和操作。在编写SQL代码时,可以根据具体的业务需求灵活选择简单形式或搜索形式来进行编写,这样可以大大提高编码效率和代码可读性。

附:常见使用场景,如:二次定义标签、饱和度统计、计算指标、数据格式转换等。

——结束

Guess you like

Origin blog.csdn.net/GaussDB/article/details/133981968