Skip to content
Advertisement

How to pivot in BigQuery?

I’m doing validations between source tables and destination tables in different processes in a Data Lake.

I work with StandardSQL in BigQuery, and I do the comparisons between both tables with this query:

And this is the result:

enter image description here

Is there a way to normalize and denormalize this information as follows?

enter image description here

I have to evaluate several processes that have more or less measures, and I would need to pivot the query to compare more easily and in a standardized way for all the processes.

Thanks and regards.

UPDATE: Without creating tables or views. I need it to be with a query.


UPDATE2:

In case of evaluating 3 or more tables, I would need the query to be able to join the process column (with the name of the table) according to the evaluated measures.

This would be the result of the previous query:

enter image description here

and this should be the result:

enter image description here

Is this possible?

Thanks and regards.

Advertisement

Answer

Use CROSS JOIN to ‘explode’ the rows, so you have a row for each measure.

Use ‘conditional aggregation’ to ‘collapse’ pairs of rows in to one row with two columns.

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