-2
code  name   value
1     aaa    15
2     bbb    18
1     aaa    17
1     aaa    16

I need select items group by code. Result must be:

1   aaa   15,17,16
2   bbb   18
user2759163
  • 1
  • 1
  • 3

2 Answers2

0

You need string_agg as follows:

Select code, name,
       String_agg(value,',') within group (order by value) as val
  From t
 Group by code, name
Popeye
  • 35,427
  • 4
  • 10
  • 31
0

If you have a maximum of two values, you can use aggregation:

select code, name,
       (case when count(*) > 1 then concat(min(value), ',', max(value))
             else min(value)
        end)
from t
group by code, name;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786