[英]Grouping rows from the same table into a row from same table
表A
假設我有
group_id my_id value
1 a 10
1 b 15
2 c 20
3 d 25
我想輸出
group_id my_id value family
1 a 10 [10, 15]
1 b 15 [10, 15]
2 c 20 [20]
3 d 25 [25]
使用array_agg並加入
select t1.*,t2.family from tbl t1
join
(
select group_id,array_agg(value) as family form tbl
group by group_id
) as t2 t1.group_id=t2.group_id
您可以使用相關的標量子查詢:
select group_id,
value,
array(select value from the_table t2 where t2.group_id = t1.group_id) as family
from the_table t1
order by group_id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.