Skip to content
Advertisement

Summing totals from nested and unnested data in a single query

I am working on nested Google Analytics data to build a query, I need to unnest 3 levels in order to get all the fields I need, but once I have unnested the SUM() of my .totals fields are far too high, I assume because their values are being repeated.

I have no way if identifying, for example, bounces, at hit level, so I need to use totals.bounces to get this value.

How could I adjust my query below to get the correct sum for bounces and revenue, alongside my totals for the unnested values?

Advertisement

Answer

Now that I have a little more experience with BigQuery, I can answer this question based on how I achieve this today.

Using WITH() I create multiple queries and includes onto the fields I need at those levels, for example, my first WITH() statement will have no unnesting and will correctly sum the totals. fields. A second WITH() can then UNNEST() the hits level and sum the fields I want to count within here.

These queries can then be united with a common join and will show the correct values, without duplication, at each level of the unnesting.

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