-1

I have a table where all the records are identical, besides the values in one column. Something like this:

City Warehouse OrderID CustomerID
San Diego SD1 1234 5678
San Diego SD2 1234 5678
San Diego SD3 1234 5678

What SQL syntax/logic can I use in order to return one singular record, where the unique values from the Warehouse column are combined into one row?

Expected output:

City Warehouse OrderID CustomerID
San Diego SD1, SD2, SD3 1234 5678
nz426
  • 91
  • 1
  • 10

2 Answers2

0

You can use the string_agg aggregate function. If the code that loads this value will decode back to an array of strings, then you can use the jsonb_agg aggregate function that will aggregate in a JSON array.

https://www.postgresql.org/docs/13/functions-aggregate.html

0

You can use the following query. Group records by City, then use the STRING_AGG function.

SELECT City, STRING_AGG(Warehouse, ',') AS Warehouse, OrderID, CustomerID
FROM yourTable
GROUP BY City,OrderID,CustomerID
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17