How can I distinct a set of rows with collapsed columns in postgresql? -
for instance, have table
id_plant | name | petals ----------+------+-------- 12 | foo | 3 13 | bar | 3 14 | foo | 6
i need distinct name , have array petals values. entry foo
:
result :
name | petals ------+-------- foo | [3, 6]
use array_agg().
with my_table(id_plant, name, petals) ( values (12, 'foo', 3), (13, 'bar', 3), (14, 'foo', 6) ) select name, array_agg(petals) petals my_table group name; name | petals ------+-------- foo | {3,6} bar | {3} (2 rows)
Comments
Post a Comment