Aut Mur
Aut Mur

Reputation: 69

Return multiple categories in a single row & single column

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

Answers (1)

Mikhail Berlyant
Mikhail Berlyant

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

enter image description here

Upvotes: 2

Related Questions