0

I have this table in PostgreSQL:

id  related
1     FT5G
2     FT5G
5     RT5G
16     B5
8    RT5G

I want to group by related and then get in one column list of all it's ids. Expected result is:

related  ids
FT5G      1,2
RT5G      5,8
B5        16

I know it should be:

select   ?
from tabl
group by related

This will give the groups, but I don't know how to specify the ids in list for each group.

avi
  • 1,626
  • 3
  • 27
  • 45
  • 2
    change your `SELECT` to : `SELECT related, string_agg(id, ',') as ids` – Elad Apr 09 '17 at 05:59
  • 3
    Possible duplicate of [How to concatenate strings of a string field in a PostgreSQL 'group by' query?](http://stackoverflow.com/questions/43870/how-to-concatenate-strings-of-a-string-field-in-a-postgresql-group-by-query) – krokodilko Apr 09 '17 at 08:50

0 Answers0