云数据库 GaussDB-UNION,CASE和相关构造:示例

时间:2023-11-01 16:22:50

示例

示例1:Union中的待定类型解析。这里,unknown类型文本'b'将被解析成text类型。

123456
openGauss=# SELECT text 'a' AS "text" UNION SELECT 'b'; text------ a b(2 rows)

示例2:简单Union中的类型解析。文本1.2的类型为numeric,而且integer类型的1可以隐含地转换为numeric,因此使用这个类型。

123456
openGauss=# SELECT 1.2 AS "numeric" UNION SELECT 1; numeric---------       1     1.2(2 rows)

示例3:转置Union中的类型解析。这里,因为类型real不能被隐含转换成integer,但是integer可以隐含转换成real,那么联合的结果类型将是real。

123456
openGauss=# SELECT 1 AS "real" UNION SELECT CAST('2.2' AS REAL); real------    1  2.2(2 rows)

示例4:TD模式下,coalesce参数输入int和varchar类型,那么解析成varchar类型。ORA模式下会报错。

--在oracle模式下,创建oracle兼容模式的数据库oracle_1。openGauss=# CREATE DATABASE oracle_1 dbcompatibility = 'ORA';--切换数据库为oracle_1。openGauss=# \c oracle_1--创建表t1。oracle_1=# CREATE TABLE t1(a int, b varchar(10));

--删除表。oracle_1=# DROP TABLE t1;--切换数据库为postgres。oracle_1=# \c postgres--在TD模式下,创建TD兼容模式的数据库td_1。openGauss=# CREATE DATABASE td_1 dbcompatibility = 'TD';--切换数据库为td_1。openGauss=# \c td_1--创建表t2。td_1=# CREATE TABLE t2(a int, b varchar(10));--查看coalesce参数输入int和varchar类型的查询语句的执行计划。td_1=# EXPLAIN VERBOSE select coalesce(a, b) from t2;                                      QUERY PLAN--------------------------------------------------------------------------------------- Data Node Scan  (cost=0.00..0.00 rows=0 width=0)   Output: (COALESCE((t2.a)::character varying, t2.b))   Node/s: All datanodes   Remote query: SELECT COALESCE(a::character varying, b) AS "coalesce" FROM public.t2(4 rows)--删除表。td_1=# DROP TABLE t2;--切换数据库为postgres。td_1=# \c postgres--删除Oracle和TD模式的数据库。openGauss=# DROP DATABASE oracle_1;openGauss=# DROP DATABASE td_1;

示例5:ORA模式下,将整个表达式最终的返回值类型定为result1的数据类型,或者与result1同类型范畴的更高精度的数据类型。

--在ORA模式下,创建ORA兼容模式的数据库ora_1。openGauss=# CREATE DATABASE ora_1 dbcompatibility = 'A';--切换数据库为ora_1。openGauss=# \c ora_1--开启Decode兼容性参数。set sql_beta_feature='a_style_coerce';--创建表t1。ora_1=# CREATE TABLE t1(c_int int, c_float8 float8, c_char char(10), c_text text, c_date date);--插入数据。ora_1=# INSERT INTO t1 VALUES(1, 2, '3', '4', date '12-10-2010');--result1类型为char,defresult类型为text,text精度更高,返回值的类型由char更新为text。ora_1=# SELECT decode(1, 2, c_char, c_text) AS result, pg_typeof(result) FROM t1; result | pg_typeof --------+----------- 4      | text(1 row)--result1类型为int,属于数值类型范畴,返回值的类型置为numeric。ora_1=# SELECT decode(1, 2, c_int, c_float8) AS result, pg_typeof(result) FROM t1; result | pg_typeof --------+-----------      2 | numeric(1 row)--不存在defresult数据类型向result1数据类型之间的隐式转换,报错处理。ora_1=# SELECT decode(1, 2, c_int, c_date) FROM t1;ERROR:  CASE types integer and timestamp without time zone cannot be matchedLINE 1: SELECT decode(1, 2, c_int, c_date) FROM t1;                                   ^CONTEXT:  referenced column: c_date--关闭Decode兼容性参数。set sql_beta_feature='none';--删除表。ora_1=# DROP TABLE t1;DROP TABLE--切换数据库为postgres。ora_1=# \c postgres--删除ORA模式的数据库。openGauss=# DROP DATABASE ora_1;DROP DATABASE
support.huaweicloud.com/distributed-devg-v2-opengauss/gaussdb-v5r2c10-0473.html