'Concat rows based on condition [duplicate]

I'm working with a table that has values like this

ID Name Value
123 Size 1
123 Size 2
123 Size 3
123 Type Shoes
234 Size 6
234 Size 7
234 Type Shirt

I want to CONCAT the answers if the name = size. So, I'd love to do this:

ID Name Value
123 Size 1, 2, 3
123 Type Shoes
234 Size 6, 7
234 Type Shirt

My thought was to use a case when size = 'name' then concat? But I'm not sure if this is the correct use. Thank you so much.



Solution 1:[1]

You can use string_agg:

SELECT id, name, string_agg(value, ',')
  FROM a
 GROUP BY id, name
 ORDER BY 1,2

Output:

ID Name value
123 Size 1,2,3
123 Type Shoes
234 Size 6,7
234 Type Shirt

Here is a dbfiddle showing how it works. This is assuming that there is only one column with name = Type, if there are more this will merge those too.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 flwd