与postgres中的distinct不同的string_agg问题

use*_*426 17 postgresql

我收到以下查询的错误.基本上要||和' distinct一起使用.

select string_agg( 'pre' || distinct user.col, 'post')
Run Code Online (Sandbox Code Playgroud)

它工作得很好

select string_agg( 'pre' || user.col, 'post')
Run Code Online (Sandbox Code Playgroud)

& 这个

select string_agg(distinct user.col, 'post')
Run Code Online (Sandbox Code Playgroud)

Clo*_*eto 28

select string_agg(distinct 'pre' || user.col, 'post')
Run Code Online (Sandbox Code Playgroud)

由于上面将拒绝在distinct聚合中使用索引'pre'取出

select 'pre' || string_agg(distinct user.col, 'postpre')
Run Code Online (Sandbox Code Playgroud)

  • 这会在整个 string_agg 输出前面添加一个“pre”字符串,例如 `preFIRSTpostSECONDpostTHIRDpost`。最初的问题(虽然不是很清楚)似乎要求在聚合中包含的每个项目前面加上“pre”,例如“preFIRSTpostpreSECONDpostpreTHIRDpost”。 (2认同)