postgresql-按每个元素中的单词对数组进行排序

标签 postgresql postgresql-9.1 postgresql-9.2

有字符串数组

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

现在我想根据每个元素中的单词数对这个数组进行排序。我试过了,但没有成功。

我想要这个输出

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

我该怎么做?

最佳答案

这确实感觉很笨拙,但我现在想不出更简单的解决方案:

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;

关于postgresql-按每个元素中的单词对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17991423/

相关文章:

sql - Postgres 查询 : finding values that are not numbers

postgresql - 为什么 Postgres 中的 COMMENT 字符串不能串联?

postgresql - 启动 postgres 服务器 : creates 6 instances of postgres and stalls

java - jetty 、JNDI、PostgreSQL : Class not found

postgresql - 使用 "pg_basebackup"实用程序进行备份时收到错误

postgresql - 当参数大小超过 393166 个字符时,PSQL 准备好的语句查询会挂起

sql - 根据 id 和时间戳差异创建唯一 id

PostgreSQL:循环直到条件为真

json - 将 postgres db schema 转换为 json 格式

Postgresql树型数据结构