SELECT a.farm_id, string_agg(DISTINCT
a.name, ',') as animals, string_agg(DISTINCT
t.name, ',') as tools
FARM_ID ANIMALS TOOLS
1 cow,horse axe,hammer,spade
Greenplum DB Output:
ERROR: DISTINCT is supported only for single-argument aggregates
If I remove the distinct clauses, which is "close", the repeating values in the strings will cause problems in the downstream analysis work:
test=# SELECT a.farm_id, string_agg(
a.name, ',') as animals, string_agg(
t.name, ',') as tools
FROM animal a, tool t
WHERE a.farm_id = t.farm_id
GROUP BY a.farm_id;
farm_id | animals | tools
---------+-------------------------------+-----------------------------------
1 | cow,horse,cow,horse,cow,horse | spade,spade,axe,axe,hammer,hammer
(1 row)
CREATE TABLE animal (
id serial primary key,
farm_id integer,
name varchar
);
INSERT INTO animal (farm_id, name) VALUES (1, 'cow'), (1, 'horse');
CREATE TABLE tool (
id serial primary key,
farm_id integer,
name varchar
);
INSERT INTO tool (farm_id, name) VALUES (1, 'hammer'), (1, 'spade'), (1, 'axe');
Thanks.