我有一个列名为Item_code,其值为'RAM-1','RAM-2','RAM-2'....'RAM-12'.我必须在PostgreSQL中编写程序,从中找到最大值。
CREATE OR REPLACE FUNCTION public.get_last_itemcode(
item text)
RETURNS text
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
declare
result text;
BEGIN
Select MAX(it.item_code) into result from public.items as it where it.item_name = item;
return result;
END;
$BODY$;
但它给出的值最大值为“RAM-9”,而不是“RAM-15”。
2条答案
按热度按时间gz5pxeao1#
以下各项应满足规定的要求:
已指定
STRICT
,因为如果 item 为NULL,则函数将始终返回NULL。查询中的所有列和变量引用都经过限定,以防止名称冲突。类型声明引用相关的表列,以帮助防止不兼容的架构更改。q3qa4bjr2#
要获得最大值,您需要按照从
item_code
中提取的数字对记录进行降序排列,然后使用LIMIT 1
获取第一个:Demo here