postgresql-sort array by words in each elements

你。 提交于 2019-12-24 00:53:16

问题


There is string array

ARRAY['CAT','CAT DOG CAT','DOG Cat']

Now i want to sort this array on count of words in each element. I have tried but cannot get any success.

I want this output

ARRAY['CAT DOG CAT','DOG CAT','Cat']

How i can do this?


回答1:


This does feel rather clumsy, but I can't think of a simpler solution right now:

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;


来源:https://stackoverflow.com/questions/17991423/postgresql-sort-array-by-words-in-each-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!