0

I'm not sure how to call what I'm trying to do, so trying to look it up didn't work very well. I would like to aggregate my table based on one column and have all the rows from another column collapsed into an array by unique ID.

| ID | some_other_value |
-------------------------
| 1  | A                |
| 1  | B                |
| 2  | C                |
| .. | ...              |

To return

| ID | values_array     |
-------------------------
| 1  | {A, B}           |
| 2  | {C}              |

Sorry for the bad explanation, I'm really lacking the vocabulary here. Any help with writing a query that achieves what's in the example would be very much appreciated.

2 Answers 2

1

Try the following.

select id, array_agg(some_other_value order by some_other_value ) as values_array from <yourTableName> group by id

You can also check here.

Sign up to request clarification or add additional context in comments.

Comments

1

See Aggregate Functions documentation.

SELECT
    id,
    array_agg(some_other_value)
FROM
    the_table
GROUP BY
    id;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.