如何通过另一个表中的 id 数组聚合一个表中的文本?



我使用的是Postgresql 11。我有两个表:

Report_template

<表类> id(int) template_blocks_id(int []) tbody><<tr>11、2、323、2、132, 2, 3, 143, 1

您需要连接到unnest的结果,以便重复行:

select rt.id as report_template_id, 
string_agg(rtb.code, ',' order by b.idx) as aggregated_code
from report_template rt
cross join unnest(rt.template_blocks_id) with ordinality as b(template_id, idx)
join report_template_block rtb on rtb.id = b.template_id
group by rt.id
order by rt.id;