0

I want to aggregate two array rows. For example, in the table below, colour column already shows the values in array format.

id colour
1 [[Red,Blue,Yellow]]
2 [[Green]]
1 [[White,Blue]]
2 [[Green,Black]]

By aggregating on id and distinct-selecting the colour column, I want to create a result like below.

id colour
1 [[Red,Blue,Yellow,White]]
2 [[Green,Black]]

How do I achieve this?

2
  • Wich DB Product are you using? Commented May 19, 2023 at 15:36
  • It's Amazon Athena Commented May 19, 2023 at 15:42

1 Answer 1

3

Presto has lots of array functions. We can try:

select id, array_distinct(flatten(array_agg(colour))) as colours
from mytable
group by id

This assumes that each array contains a list of scalars, so something like: [Red,Blue,Yellow] - not a one element sub-array (as shown in your sample data like [[Red,Blue,Yellow]]).

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

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.