Reputation: 69
I have a table of stores that sell to multiple categories and I'm looking to return a single row for each store, with the categories listed in one cell.
My table:
store | category |
---|---|
a | clothing |
b | supplies |
c | food |
a | supplies |
a | food |
b | clothing |
What I'm looking for:
store | category |
---|---|
a | clothing, food, supplies |
b | clothing, supplies |
c | food |
Any advice would be greatly appreciated!
Upvotes: 0
Views: 297
Reputation: 173003
Use below
select store, string_agg(category, ', ') category
from your_table
group by store
if applied to sample data in your question - output is
Upvotes: 2