Skip to content
Advertisement

Combine 2 sql queries in Bigquery

I currently have two queries that i’ve stitched together using views in Bigquery. I am wondering if there’s a way to combine them into a single query and eliminate the need for multiple views.

The first query concatenates a few strings to create a field “id”

The second query de-dupes the records based on the id field.

thank you in advance

Advertisement

Answer

With BigQuery, you can use the “UNION ALL” statement, but you need to follow some rules in both queries to use it.

The rules are:

  • Both queries must have the same number of columns.
  • Both queries must have the same data type.

You can see this sample code using “UNION ALL:”

Both have the same number of columns and the same data type.

In this case, you can see a sample code that is wrong:

The query is invalid since all the queries contain the same number of columns, but columns are of different data types (first two are integer and comp as string). So we have to convert it into the same data type.

You can see more information about “UNION ALL” in BigQuery.

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