How to aggregate two PostgreSQL columns to an array separated by brackets

Mattijn

I would like to concatenate two columns using a group-by query resulting in an array separed with brackets. I know this question is related to this question, but as usual my use-case is a little different.

A simple example (also as SQL Fiddle). Currently my query returns the following:

ID  X   Y
3   0.5 2.71
3   1.0 2.50
3   1.5 2.33
6   0.5 2.73
6   1.5 2.77

But where I would like concatenate/aggregate the X/Y columns to get the following:

ID  XY
3   [[0.5,2.71],[1.0,2.50],[1.5,2.33]]
6   [[0.5,2.73],[1.5,2.77]]

Currently I've tried to concatenate the columns into one as follows:

SELECT "ID",concat_ws(', ',"X", "Y") as XY FROM Table1;

Which returns:

ID  xy
3   0.5, 2.71
3   1, 2.50
3   1.5, 2.33
6   0.5, 2.73

And used array_agg():

SELECT "ID",array_to_string(array_agg("X"),',') AS XY
FROM Table1
GROUP BY "ID";

Resulting in:

ID  xy
3   0.5,1,1.5
6   0.5

I feel I'm getting closer, but a helping hand would be really appreciated.

a_horse_with_no_name

Create an array from the two columns, the aggregate the array:

select id, array_agg(array[x,y])
from the_table
group by id;

Note that the default text representation of arrays uses curly braces ( {..}) not square brackets ([..])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PostgreSQL: Efficiently aggregate array columns as part of a group by

How to separate two strings separated by a ; into two columns

How to aggregate columns into a JSON array?

Postgresql - How to SELECT using a json aggregate function that takes two columns as key: value pair?

How to subtract two comma separated columns in R?

How to print array lists comma separated with curly brackets?

How to aggregate two tables on a comma-separated string field?

How to convert a comma separated string to an array in postgresql

How to aggregate the average of a calculation based on two columns?

How to aggregate by names in two columns in R?

How canI aggregate and group in two columns?

MySQL: In a select with aggregate functions, how to join two columns not in aggregate functions?

how to sum two columns in postgresql?

pandas: how to create new columns based on two columns and aggregate the results

pandas - How to aggregate two columns and keeping all other columns

R - aggregate two columns

How to aggregate 3 columns in DataFrame to have count and distribution of values in separated columns in Python Pandas?

How to aggregate two different columns with two different functions in R dataframe

How to aggregate objects that exist in two array?

PostgreSQL: Aggregate into array and join

How to aggregate data and find min,max,avg of numeric columns in postgresql

Aggregate multiple columns into array

How to extract two different columns separated by different delimiters into the same line

How to combine two columns with an array

How to conditional sum two columns in PostgreSQL 9.3

how to combine two columns of integer in PostgreSQL?

PostgreSQL - How to get data grouped by two columns?

postgresql how to check by two columns from join

How to combine the value of two columns in PostgreSQL?