-3
I have a table like this:
id  name
1   washing
1   cooking
1   cleaning
2   washing
2   cooking
3   cleaning

and I would like to have a following grouping

id  name                      count
1   washing,cooking,cleaning    3
2   washing,cooking             2
3   cleaning                    1

I have tried to group by ID but can only show count after grouping by

SELECT id,
       COUNT(name)
FROM WORK
GROUP BY id

But this will only give the count and not the actual combination of names.

I am new to SQL. I know it has to be relational but there must be some way.

Thanks in advance!

Fact
  • 1,957
  • 1
  • 17
  • 26

1 Answers1

0

in postgresql you can use array_agg

SELECT id, array_agg(name),  COUNT(*)
FROM WORK
GROUP BY id

in mysql you can use group_concat

SELECT id, group_concate(name),  COUNT(*)
FROM WORK
GROUP BY id

or for redshift

SELECT id, listagg(name),  COUNT(*)
FROM WORK
GROUP BY id
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107