0

Let's suppose I have this table:

1|A|B|10
2|A|B|20
3|B|C|20

How to write a query to get this value:

1|A|B|10,20
3|B|C|20
  • what have you tried yourself so far? what problems did you encounter? – odaiwa Oct 10 '22 at 06:46
  • 1
    Does this answer your question? [How to concatenate strings of a string field in a PostgreSQL 'group by' query?](/q/43870/90527) – outis Oct 10 '22 at 07:01

1 Answers1

0

Something like this should work:

SELECT MIN(column_1) AS min_value
, column_2
, column_3
, STRING_AGG(column_4 ORDER BY column_4, ' ')
FROM table_name
GROUP BY column_2, column_3
ORDER BY MIN(column_1);
Frank Heikens
  • 117,544
  • 24
  • 142
  • 135
  • 1
    Judging by the expected output in the question, the OP is probably looking for `string_agg()` rather than `array_agg()` –  Oct 10 '22 at 07:06