Skip to content
Advertisement

Is there an elegant way to aggregate data by groups based on elements of an array in SQL?

I’m looking for a way to aggregate data by groups that are based on elements in an array. I’m working with product and sales data in a BigQuery warehouse, and want to measure the sales of collections of products, where each product may be in more than one collection.

This is probably best explained with a toy example:

Where the collections column above is an array of strings.

I want to get total sales for each collection:

Now I know I could do these one at a time with something like

or

But I’m looking for something more elegant that can handle lots and lots of collections and returns a table like the one above. Thank you all!

Advertisement

Answer

Consider below

if applied to sample data in your question (… collections column … is an array of strings.)

output is

enter image description here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement