GaussDB SQL basic syntax example-common conditional expressions

Table of contents

I. Introduction

2. The concept of conditional expressions and common conditional expressions in GaussDB

3. Commonly used conditional expressions in GaussDB (grammar + examples)

1. CASE expression

2. DECODE expression

3. COALESCE expression

4. NULLIF expression

5. GREATEST/LEAST expression

6. NVL expression

4. 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 "Cloud Database GaussDB—SQL Reference".

2. The concept of conditional expressions and common conditional expressions in GaussDB

Conditional expressions refer to expressions used to filter out data that meets requirements based on specific conditions when performing SQL statement queries in the database. In the GaussDB database, CASE, DECODE, COALESCE, NULLIF, GREATEST and NVL are all commonly used conditional expressions.

  • CASE: Make multi-branch judgments based on conditions and return different results according to different conditions.
  • DECODE:The function function provided by the GaussDB database is equivalent to the IF-THEN-ELSE statement in the SQL language, according to the first parameter Compare with subsequent parameters and return results that meet the conditions.
  • COALESCE:Returns the first non-empty parameter value. If all parameters are empty, NULL will be returned. COALESCE will not calculate parameters that are not needed to determine the result; that is, the parameters to the right of the first non-null parameter will not be calculated.
  • NULLIF: is used to compare the values ​​​​of two fields. If they are equal, NULL is returned, otherwise the value of the first field is returned. . The data types of the two expressions are required to be consistent.
  • GREATEST: is used to return the maximum value among multiple numeric values.
  • NVL: accepts two parameters. If the first parameter is empty, the value of the second parameter is returned; if the first parameter If the two parameters are not empty, the value of the first parameter is returned.

They will be introduced one by one below.

3. Commonly used conditional expressions in GaussDB (grammar + examples)

1, CASE expression

1)Language:

CASE WHEN condition1 THEN result1

           WHEN condition2 THEN result2

           ……

           ELSE result

 END

Description: Perform multi-branch judgment based on conditions and return different results according to different conditions.

  • If the result is true, the result of the CASE expression is the result corresponding to the condition.
  • If the result is false, subsequent WHEN or ELSE clauses are processed in the same way.
  • If none of the WHEN conditions is true, the result of the expression is the result executed in the ELSE clause. If the ELSE clause is omitted and there are no matching conditions, the result is NULL.

2) Example:

SELECT name

              ,age

              ,CASE

                         WHEN age < 18 THEN 'Underage'

                         WHEN age >= 18 AND age < 60 THEN '成年'

                         

               END AS age_group

FROM company;

Analysis:

This code mainly selects the "name" and "age" data from the "company" table, and uses the "CASE expression" to generate a group based on age for each user ("underage" ', 'adult' or 'elderly').

See also, the previous article "GaussDB SQL basic syntax example-CASE expression".

2, DECODE expression

1)Language:

DECODE(base_expression,compare1,value1,compare(n),value(n),default)

Note: The function function provided by the GaussDB database is equivalent to the IF-THEN-ELSE statement. It compares the first parameter and subsequent parameters and returns a result that meets the conditions.

2) Example:

SELECT name

              ,salary

              ,DECODE(salary, NULL, 'Unknown', 5000, 'Junior Standard Line', 20000, 'Intermediate Standard Line',30000, ' ;Advanced level standard line', 'Other') AS salary_level

FROM company ORDER BY salary ;

Analysis:

The purpose of this code is to select the "name" and "salary" data from the "company" table and define the salary level ("unknown", "unknown", "salary") through the "DECODE expression" Junior Standard Line', 'Intermediate Standard Line', 'Advanced Standard Line', 'Others').

3, COALESCE expression

1) Grammar:

COALESCE(value1,value2,…)

Description: Returns the first non-null parameter value. If all parameters are empty, NULL will be returned. COALESCE will not calculate parameters that are not needed to determine the result; that is, the parameters to the right of the first non-null parameter will not be calculated.

2) Example:

SELECT name

              ,COALESCE(address, 'Unknown address') AS address

FROM company;

Parsing:This query will return the name and address of each employee, judged by the "expression COALESCE". If the address is unknown, it will display "Unknown Address" #39;.

4, NULLIF expression

1) Grammar:

NULLIF(value1,value2)

Description: Used to compare the values ​​​​of two fields. If they are equal, NULL is returned, otherwise the value of the first field is returned. The data types of the two expressions are required to be consistent.

2) Example:

SELECT NULLIF('abc', 'abc'); -- returns NULL

SELECT NULLIF('abc', '123'); -- returns '123'

5、GREATEST/ LEAST表达式

1) Grammar:

GREATEST(value1,value2,…)

LEAST(value1,value2,…)

说明:GREATEST用于返回多个数字值中的最大值。LEAST,从一个任意数字表达式的列表里选取最小的数值。以上的数字表达式必须都可以转换成一个普通的数据类型。

2)示例:

SELECT GREATEST(10, 20, 30); -- 返回30

SELECT LEAST(10, 20, 30); -- 返回10

6、NVL表达式

1)语法:

NVL(value1,value2,…)

说明:接受两个参数,如果第一个参数为空,则返回第二个参数的值;如果第一个参数不为空,则返回第一个参数的值,参数类型必须一致。

2)示例:

SELECT name

              ,salary

              ,NVL(salary,default_salary) AS d_salary

FROM company;

解析:这个查询将返回每个员工的名字和薪水,通过“表达式NVL”判断,如果salary为空,则用取默认的default_salary 。

四、小结

条件表达式是数据库查询中非常有用的工具,今天介绍的CASE、DECODE、COALESCE、NULLIF、GREATEST和NVL等,在GaussDB数据库中是非常常用的,通过使用这些条件表达式,我们可以更加灵活地对数据进行查询和操作,提高程序的效率和可读性。同时,它们也使得数据的处理更为方便和快捷,为数据分析和决策提供了有力的支持。

--结束

Guess you like

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