Home > other >  Return multiple categories in a single row & single column
Return multiple categories in a single row & single column

Time:01-15

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!

CodePudding user response:

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

  •  Tags:  
  • Related