Postgresql – Postgres Grouping of Like Results

postgresqlpostgresql-9.3

Our database is running on Postgres 9.3. We have a table called notifications.

Notifications contains id, type, triggering id, source id, and some other information.

What I am trying to do is select the notifications and then group them on their source ID.

So if I have the following:

id     type     source_id     triggering_id
1     comment     1234        4567
2     comment     1234        0123
3     comment     1234        5432

I would like to get the following back:

source_id, triggering_id(s)

I don't know if this is even possible. I'm pretty new to the DBA stuff.

Best Answer

If I understand you correctly you want a comma separated list of triggering_id's

select source_id, 
       string_agg(triggering_id::text,',') as id_list
from notifications
group by source_id;