简体   繁体   中英

Query distinct on multiple columns and aggregate others in Postgres

I have a table like this:

X,Y,Z,A,B,C
X,Y,Z,D,E,F
X,Y,Z,G,H,I
X,Y,W,A,C,D
X,Y,W,H,I,J

Obviously it is non-normalized table. I want a query that returns the following result:

X,Y,Z,{{A,D,G},{B,E,H},{C,F,I}}
X,Y,W,{{A,H},{C,I},{D,J}}

Normalization is very expensive.Any ideas?

try this:

SELECT c1,c2,c3,ARRAY[array_agg(c4),array_agg(c5)]
from your_table
group by c1,c2,c3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM