I stedet for at bruge vinduesfunktioner og patitionering skal du bruge en GROUP BY på forespørgselsniveau og samle med en DISTINCT-sætning:
SELECT
rnp.grp_id,
array_to_string(array_agg(distinct rnp.cabinets),',') AS cabinets,
array_to_string(array_agg(distinct ips.address),',') AS addresses
FROM rnp JOIN ips ON rnp.grp_id=ips.grp_id GROUP BY rnp.grp_id, ips.grp_id;
Resultat:
grp_id | cabinets | addresses
--------+-------------------------+-----------
11 | cabs1,cabs2,cabs3,cabs4 | CA,NY
22 | c1,c2 | DC,LA
(2 rows)
Nøglen her er, at i stedet for at bruge vinduesfunktioner og patitionering, bruger du en GROUP BY
forespørgselsniveau og aggregér med en DISTINCT
klausul.
Dette ville også fungere med vinduesfunktionstilgangen, bortset fra at PostgreSQL (i det mindste 9.1) ikke understøtter DISTINCT
i vinduesfunktioner:
regress=# SELECT DISTINCT
rnp.grp_id,
array_to_string(array_agg(distinct rnp.cabinets)OVER (PARTITION BY rnp.grp_id), ',') AS cabinets,
array_to_string(array_agg(distinct ips.address) OVER (PARTITION BY ips.grp_id), ',') AS addresses
FROM rnp JOIN ips ON rnp.grp_id=ips.grp_id;
ERROR: DISTINCT is not implemented for window functions
LINE 3: array_to_string(array_agg(distinct rnp.cabinets)OVER (PART...