[英]Merging text rows into an array in psql when another row values match
我當前的表格如下所示:
id | col1 | col2
1 | a | z
2 | b | y
3 | a | w
4 | c | x
5 | b | z
我想根據 col1 中的重復值將 col2 中的值組合成一個數組,使我的 col1 唯一。 我預期的 output 是這樣的:
id | col1 | col2
1 | a | {z, w }
2 | b | {y, z }
4 | c | { x }
我不確定如何在 postgres 中執行此操作。 謝謝
使用array_agg()
:
select min(id), col1, array_agg(col2)
from t
group by col1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.