如何将重复的列数据移动到行中

问题描述 投票:0回答:1

我该如何从下表中的col3重写数据:

col1    col2    col3    
1       1       5    
1       2       3    
1       2       4    
1       2       4    
1       2       6    
1       1       5    
1       2       7    
1       3       7

成为

col1     col2   col3    col4    col5    col6    col7    colN
1       1       5       5
1       2       3       4       4       6       7
1       3       7
sql postgresql pgadmin
1个回答
0
投票

我建议您将结果放入array,而不要放在单独的列中。为了将数据放在单独的列中,您将需要某种类型的动态SQL。

所以,也许这符合您的需求:

select col1, col2, array_agg(col3 order by col3)
from t
group by col1, col2;
© www.soinside.com 2019 - 2024. All rights reserved.