我需要 postgresql 中的聚合函式,它回傳文本列的最大值,其中最大值不是按字母順序計算的,而是按字串的長度計算的。
誰能幫幫我?
uj5u.com熱心網友回復:
自定義聚合由兩部分組成:完成作業的函式和聚合函式的定義。
所以我們首先需要一個函式來回傳兩個字串中較長的一個:
create function greater_by_length(p_one text, p_other text)
returns text
as
$$
select case
when length(p_one) >= length(p_other) then p_one
else p_other
end
$$
language sql
immutable;
然后我們可以使用該函式定義一個聚合:
create aggregate max_by_length(text)
(
sfunc = greater_by_length,
stype = text
);
并使用它:
select max_by_length(s)
from (
values ('one'), ('onetwo'), ('three'), ('threefourfive')
) as x(s);
回傳threefourfive
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/479994.html
