Skip to content
Advertisement

Efficient Multiple Group-bys

I have the following table:

Year Week Day_1 Day_2 Day_3
2020 1 Walk Jump Swim
2020 3 Walk Swim Walk
2020 1 Jump Walk Swim

I want to group by YEAR, WEEK and Event (Walk, jump, Swim) and count the number of times each event occurs in Day_1, Day_2, Day_3. I.e.

Year Week Event Count_Day_1 Count_Day_2 Count_Day_3
2020 1 Walk 1 1 0
2020 3 Walk 1 0 1
2020 1 Jump 1 1 0
2020 3 Jump 0 0 0
2020 1 Swim 0 0 2
2020 3 Swim . 0 1 0

How can I do this efficiently?

Advertisement

Answer

In BigQuery, I would unpivot using arrays and then aggregate:

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