openGauss Expressions
An expression is similar to a formula. You can use it in a query statement to search for the result set of a specified condition in a database.
Simple Expressions
Logical expressions
Logical Operators lists the operators and computation rules of logical expressions.
Comparison expressions
Operators lists the common comparison operators.
In addition to comparison operators, you can also use the following sentence structures:
BETWEEN operator
a BETWEEN x AND y is equivalent to a >= x AND a <= y.
a NOT BETWEEN x AND y is equivalent to a < x OR a > y.
To check whether a value is NULL, use:
expression IS NULL
expression IS NOT NULL
or an equivalent (non-standard) sentence structure:
expression ISNULL
expression NOTNULL
NOTICE: Do not write expression=NULL or expression<>(!=)NULL, because NULL represents an unknown value, and these expressions cannot determine whether two unknown values are equal.
is distinct from/is not distinct from
is distinct from
If the data types and values of A and B are not completely the same, the value is true.
If the data types and values of A and B are completely the same, the value is false.
Null values are considered the same.
is not distinct from
If the data types and values of A and B are not completely the same, the value is false.
If the data types and values of A and B are completely the same, the value is true.
Null values are considered the same.
Pseudocolumn (ROWNUM)
ROWNUM is a pseudocolumn that returns a number indicating the row number of the result obtained from the query. The value of ROWNUM in the first row is 1, the value of ROWNUM in the second row is 2, and so on. The return type of ROWNUM is numeric. ROWNUM can be used to limit the total number of rows returned by a query. For example, the following statement limits the maximum number of records returned from the customer_t1 table to 3.
""openGauss=# SELECT * FROM customer_t1 WHERE ROWNUM <= 3; c_customer_sk | c_customer_id | c_first_name | c_last_name | amount ---------------+---------------+--------------+-------------+-------- 3869 | hello | Grace | | 1000 3869 | hello | Grace | | 1000 3869 | | Grace | | (3 rows)
Condition Expressions
Data that meets the requirements specified by conditional expressions are filtered during SQL statement execution.
Conditional expressions include the following types:
CASE
CASE expressions are similar to the CASE statements in other programming languages.
A CASE clause can be used in a valid expression. condition is an expression that returns a value of Boolean type.
If the result is true, the result of the CASE expression is the required result.
If the result is false, the following WHEN or ELSE clauses are processed in the same way.
If every WHEN condition is false, the result of the expression is the result of the ELSE clause. If the ELSE clause is omitted and has no match condition, the result is NULL.
Example:
Note: If the tpcds schema does not exist, create the tpcds schema and then create the table.
openGauss=# CREATE TABLE tpcds.case_when_t1(CW_COL1 INT);
openGauss=# INSERT INTO tpcds.case_when_t1 VALUES (1), (2), (3);
openGauss=# SELECT * FROM tpcds.case_when_t1;
cw_col1
---------
1
2
3
(3 rows)
openGauss=# SELECT CW_COL1, CASE WHEN CW_COL1=1 THEN 'one' WHEN CW_COL1=2 THEN 'two' ELSE 'other' END FROM tpcds.case_when_t1 ORDER BY 1;
cw_col1 | case
---------+-------
1 | one
2 | two
3 | other
(3 rows)
openGauss=# DROP TABLE tpcds.case_when_t1;
DECOD
Compare each following compare(n) with base_expr. value(n) is returned if a compare(n) matches the base_expr expression. If base_expr does not match each compare(n), the default value is returned.
Example:
openGauss=# SELECT DECODE('A','A',1,'B',2,0); case ------ 1 (1 row)
COALESCE
COALESCE returns its first not-NULL value. If all the parameters are NULL, NULL is returned. This value is replaced by the default value when data is displayed. Like a CASE expression, COALESCE only calculates the parameters that are needed to determine the result. That is, parameters to the right of the first non-null parameter are not calculated.
Example:
Note: If the tpcds schema does not exist, create the tpcds schema and then create the table.
openGauss=# CREATE TABLE tpcds.c_tabl(description varchar(10), short_description varchar(10), last_value varchar(10)) ; openGauss=# INSERT INTO tpcds.c_tabl VALUES('abc', 'efg', '123'); openGauss=# INSERT INTO tpcds.c_tabl VALUES(NULL, 'efg', '123'); openGauss=# INSERT INTO tpcds.c_tabl VALUES(NULL, NULL, '123'); openGauss=# SELECT description, short_description, last_value, COALESCE(description, short_description, last_value) FROM tpcds.c_tabl ORDER BY 1, 2, 3, 4; description | short_description | last_value | coalesce -------------+-------------------+------------+---------- abc | efg | 123 | abc | efg | 123 | efg | | 123 | 123 (3 rows) openGauss=# DROP TABLE tpcds.c_tabl;
If description is not NULL, the value of description is returned. Otherwise, parameter short_description is calculated. If short_description is not NULL, the value of short_description is returned. Otherwise, parameter last_value is calculated. If last_value is not NULL, the value of last_value is returned. Otherwise, none is returned.openGauss=# SELECT COALESCE(NULL,'Hello World'); coalesce --------------- Hello World (1 row)