有字符串数组
ARRAY['CAT','CAT DOG CAT','DOG Cat']
现在我想根据每个元素中的单词数对这个数组进行排序。我尝试过但无法取得任何成功。
我想要这个输出
ARRAY['CAT DOG CAT','DOG CAT','Cat']
我怎么能这样做?
答案 0 :(得分:1)
这确实感觉相当笨拙,但我现在想不出更简单的解决方案:
with val (col) as (
values (ARRAY['CAT','CAT DOG CAT','DOG Cat'])
), word_list as (
select unnest(col) as pc
from val
), wc as (
select array_length(string_to_array(pc, ' '),1) as word_count, pc
from word_list
)
select array_agg(pc order by word_count desc)
from wc;
答案 1 :(得分:0)
我想出了一个替代方案:
select array_agg(a)
from (
select unnest(array['CAT','CAT DOG CAT','DOG Cat']) as a
order by a) s