Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Support UNION function in BigQuery SQL

BigQuery does not seem to have support for UNION yet: https://developers.google.com/bigquery/docs/query-reference

(I don't mean unioning tables together for the source. It has that.)

Is it coming soon?

like image 816
mdahlman Avatar asked May 17 '12 23:05

mdahlman


People also ask

Is UNION supported in BigQuery?

There are two main ways to unite results with UNION in BigQuery which is: Comma-Delimited UNIONS in Legacy SQL and Standard SQL. Comma-Delimited UNIONS in Legacy SQL: With this method, merging data is very easy. All you need is a comma-separated list of the many tables included inside the FROM clause.

Can we use UNION in SQL?

The SQL UNION OperatorThe UNION operator is used to combine the result-set of two or more SELECT statements.

How do you combine tables in BigQuery?

Click the join and choose Delete. Create a new join by dragging the column name from the second table to the corresponding column name in the first table. Click Execute SQL. If a preview of your data appears in the Sample Preview pane, the join was successfully created.


1 Answers

If you want UNION so that you can combine query results, you can use subselects in BigQuery:

SELECT foo, bar  FROM   (SELECT integer(id) AS foo, string(title) AS bar     FROM publicdata:samples.wikipedia limit 10),   (SELECT integer(year) AS foo, string(state) AS bar     FROM publicdata:samples.natality limit 10); 

This is almost exactly equivalent to the SQL

SELECT id AS foo, title AS bar  FROM publicdata:samples.wikipedia limit 10 UNION ALL SELECT year AS foo, state AS bar  FROM publicdata:samples.natality limit 10; 

(note that if want SQL UNION and not UNION ALL this won't work)

Alternately, you could run two queries and append the result.

like image 134
Jordan Tigani Avatar answered Sep 29 '22 13:09

Jordan Tigani