返回首页

gbase数据、南大通用产品文档:GBase8aGREATEST(value1,value2,)

更新日期:2024年09月11日

语法
GREATEST(value1,value2,...)
函数说明
当有两个或多个参数时,返回值为最大的参数值。
当参数中有一个为NULL 时,直接返回NULL。
当参数都是字符串时,默认是不区分大小写的,如果希望进行字符串取值大小写
敏感的比较,则在需要敏感的字符串参数前加上BINARY。

这些参数比较使用下列规则:

如果返回值在INTEGER 上下文中或者所有的参数是整型值,那么它们
使用整数比较;

如果返回值在REAL 上下文中或者所有的参数是实数值,那么它们使用
实数比较;

如果所有的参数是大小写敏感的字符串,那么参数比较也是大小写敏感
的;其它情况下,参数比较大小写不敏感。
示例
示例1:参数值为整型数字。
gbase> SELECT GREATEST(2,0) FROM dual;
+---------------+

GBase 8a MPP Cluster 产品手册
5 数据库管理指南
文档版本953(2022-04-10)
南大通用数据技术股份有限公司
654
| GREATEST(2,0) |
+---------------+
|
2 |
+---------------+
1 row in set
示例2:参数值为浮点型数字。
gbase> SELECT GREATEST(34.0,3.0,5.0,767.0) FROM dual;
+------------------------------+
| GREATEST(34.0,3.0,5.0,767.0) |
+------------------------------+
|

数据库对象结构导出工具gcdump .................................. 1267

问题现象
函数lpad、rpad 不支持length 函数运算改写。
解决方法

业务要求1:保持9 位,不足位数前面补0。
如:1234,结果:000001234。
select lpad(GH_FLAG,9-length(GH_FLAG),'0') from
ics.ASSE_BILL_ITEM_RESULT_4;
目前不支持这种语法,改写为:
select left('000000000',9-length(GH_FLAG))||GH_FLAG from
ics.ASSE_BILL_ITEM_RESULT_4;

业务要求2:只显示手机号前两位和姓名的第一个字符,其它以*代替。
测试表:
insert into t_01 values('13512345678','赵子龙');
1.
客户使用的是length(phone)-1,这里用户理解有误,以为是需要补
length(phone)-1 个*,length 函数后有-1,直接报错了:

GBase 8a MPP Cluster 最佳实践
5 FAQ
文档版本(2022-02-11)
南大通用数据技术股份有限公司
111
gbase> select rpad(left(phone,7),length(phone)-1,'*') as phone from t_01;
ERROR 1708 (HY000): [192.168.3.26:5050](GBA-02EX-0005) Failed to query
in gnode:
DETAIL: Query failed.
CAUSE:(GBA-01EX-700) The query includes syntax that is not supported by the
Express engine. Either restructure the query with supported syntax, or enable the
GBase Query Path in the configuration file to execute the query with reduced
performance.
SQL: SELECT /*192.168.3.25_13_32_2017-02-26_22:24:31*/ /*+ TID('79') */
rpad(left(`testdb.t_01`.`phone`, 7), (length(`testdb.t_01`.`phone`)
-
1), '*') AS
`phone` FROM `testdb`.`
2.
将-1 去除,汉字后的*显示个数也不正确:
gbase> select phone,name,
-> rpad(left(phone,7),length(phone),'*') as phone,
-> rpad(left(name,1),length(name),'*') as name
-> from t_01;
+-------------+-----------+-------------+-------------+
| phone
| name
| phone
| name
|
+-------------+-----------+-------------+-------------+
| 13512345678 | 赵子龙
| 1351234**** | 赵********
|
+-------------+-----------+-------------+-------------+
Phone:后4 位为*,正确。
name:后2 位应为*,因为length 取的是字节数所以变成了8 个*。
3.
改为character_length,直接报错了,按研发的解释,目前rpad 函数中不支持
使用character_length。

GBase 8a MPP Cluster 最佳实践
5 FAQ
文档版本(2022-02-11)
南大通用数据技术股份有限公司
112
gbase> select rpad(left(name,1),character_length(name),'*') as name from
t_01;
ERROR 1708 (HY000): [192.168.3.26:5050](GBA-02EX-0005) Failed to query in
gnode:
DETAIL: Query failed.
CAUSE:(GBA-01EX-700) The query includes syntax that is not supported by the
Express engine. Either restructure the query with supported syntax, or enable the
GBase Query Path in the configuration file to execute the query with reduced
performance.
SQL: SELECT /*192.168.3.25_13_26_2017-02-26_22:20:13*/ /*+ TID('76') */
rpad(left(`testdb.t_01`.`name`, 1), char_length(`testdb.t_01`.`name`), '*') AS
`name` FROM `testdb`.`t_01_n
4.
最终采用的方法是自定义一个函数取*个数再与显示字符拼接的方法解决此
问题:
delimiter $$
drop function if exists testdb.f_getstar $$
CREATE FUNCTION testdb.f_getstar(n INT)
RETURNS VARCHAR(100)
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE s VARCHAR(100) DEFAULT '';
myloop: LOOP
SET i = i+1;
SET s = CONCAT(s, '*');
IF i>=n THEN LEAVE myloop;
END IF;
END LOOP myloop;
RETURN s;
END$$
delimiter ;
5.
测试结果:

GBase 8a MPP Cluster 最佳实践
5 FAQ
文档版本(2022-02-11)
南大通用数据技术股份有限公司
113
gbase> select phone,name,
-> left(a.phone,2)||testdb.f_getstar(length(a.phone)-2) as
phone,
->
left(a.name,1)||testdb.f_getstar(CHARACTER_LENGTH(a.name)-1) as
name
-> from testdb.t_01 a ;
+-------------+-----------+-------------+-------+
| phone
| name
| phone
| name
|
+-------------+-----------+-------------+-------+
| 13512345678 | 赵子龙
| 13********* | 赵**
|