Source: https://commons.wikimedia.org/wiki/File:Basketball_game.jpg

Easy pivot() in BigQuery, in one step

Introducing the easiest way to get a pivot done in BigQuery. This has been one of the most recurrent BigQuery open questions. And now, thanks to dynamic SQL and stored procedures, we can finally make it easy for everyone.

Felipe Hoffa
7 min readJun 24, 2020

--

Important update: I left Google and joined Snowflake in 2020 — so I’m unable to keep my older posts updated. If you want to try Snowflake, join us — I’m having a lot of fun ❄️.

Quick pivots

Before going any further, let me show you how to call fhoffa.x.pivot() to easily pivot any table, creating a new one with the results:

That’s it. Let’s see what’s happening here. The original table has one row per sale (invoice), including the date of the transaction, the store number, and the sale amount:

Original table to be pivoted

Then if someone requests a transformation like “I want to see the total amount sold per day per store, with each store in its own column” — that’s a pivot. The code above will create a new table that looks exactly like requested:

Pivoted table, with one store per column

Let’s review each line of the above code:

  • CALL fhoffa.x.pivot(): The first thing you might notice is the keyword CALL. That’s how you run a stored procedure like this pivot(). I shared it in my dataset fhoffa.x, so you can call it at any moment too. The following lines are the parameters for that stored procedure. Note that most of these parameters strings:
  • ‘bigquery-public-data.iowa_liquor_sales.sales’: Denotes the table we want to pivot. In this case, a public table sharing the liquor sales in Iowa.
  • ‘fh-bigquery.temp.test’: The stored procedure will create (or overwrite) this table, with the pivoted results.
  • [‘date’]: An array with the list of the dimensions we want to summarize for on each row. Here it’s just date, because all we want is a summary per day.
  • ‘store_number’: This is the column name for the column with the values we want to transform into columns.
  • ‘sale_dollars’: This is the column name for the column with the values we want to summarize for each new column.
  • 30: A number to limit the number of columns to be created. Maybe we want to include only the top 30 stores? This is useful to avoid creating thousands of non-meaningful columns.
  • ‘SUM’: The aggregation strategy you want to use for the values. Usually ANY_VALUE should be the easiest one to use — but here we really want the SUM per day.
  • ‘’: Looks empty, but you could write LIMIT 10, if you wanted to only create 10 rows (useful when testing the pivots, for faster materializations).

Let’s see some variations of this with different source tables and requests:

What if we want the summary per month?

If instead of summarizing by the raw date we want a summary per month, then we can just use the row ids array to ask for this transformation:

What if we have multiple dimensions to summarize for?

In this case I want a summary for each id and date. Now it makes sense to have an array of more than one element:

Note that I changed the source table here — this is NOAA’s GHCND daily weather. With pivot() I’m getting a column for each sensor. Also instead of using SUM() here I use AVG() — because it’s what makes more sense for weather data.

What if we have a more complex case?

In this case I want to transform the name of each column — as the values in the original table include more data than I want:

The solution was to create a VIEW with a pre-transformation of the data. To clean up the name of the new columnsREGEXP_REPLACE in the VIEW gets rid of the noise — and then pivot() can use the view as a source for the transformations.

# of Stack Overflow questions per year with each column one of the top 10 tags?

  • First, create a view with an UNNEST() for the tags of each question.
  • The aggregation is just COUNT.

If you import the results to sheets, then you can easily visualize the trends:

Visualizing the # of Stack Overflow questions per year on Sheets

How-to

Creating the shared stored procedure pivot()

The biggest new trick to have these pivots: EXECUTE IMMEDIATE.

Now that BigQuery has support for EXECUTE IMMEDIATE, we can generate strings inside BigQuery and then use BigQuery to run them. In this case the code is generating two strings:

  • First it looks for a list of all the values used to generate new columns. Note that APPROX_TOP_COUNT will quickly calculate what are the most common values. This is our first string, that also includes the aggregating strategy for each pivoted row.
  • The second generated string is the query that will CREATE OR REPLACE our destination table. This string uses the previously generated string listing all the columns and aggregating strategy.
  • An auxiliary function `fhoffa.x.normalize_col_name()`: This is the first iteration of a helper that makes sure that the name of each generated column is acceptable to BigQuery.

Next steps

Thanks to

Want more?

I’m Felipe Hoffa, a Developer Advocate for Google Cloud. Follow me on @felipehoffa, find my previous posts on medium.com/@hoffa, and all about BigQuery on reddit.com/r/bigquery.

--

--

Felipe Hoffa
Felipe Hoffa

Written by Felipe Hoffa

Data Cloud Advocate at Snowflake ❄️. Originally from Chile, now in San Francisco and around the world. Previously at Google. Let’s talk data.

Responses (12)