Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery query creation without variables?

Coming from SQL Server and a little bit of MySQL, I'm not sure how to proceed on google's BigQuery web browser query tool.

There doesn't appear to be any way to create, use or Set/Declare variables. How are folks working around this? Or perhaps I have missed something obvious in the instructions or the nature of BigQuery? Java API?

like image 875
Steve A Avatar asked Mar 18 '23 16:03

Steve A


2 Answers

It is now possible to declare and set variables using SQL. For more information, see the documentation, but here is an example:

-- Declare a variable to hold names as an array.
DECLARE top_names ARRAY<STRING>;
-- Build an array of the top 100 names from the year 2017.
SET top_names = (
  SELECT ARRAY_AGG(name ORDER BY number DESC LIMIT 100)
  FROM `bigquery-public-data`.usa_names.usa_1910_current
  WHERE year = 2017
);
-- Which names appear as words in Shakespeare's plays?
SELECT
  name AS shakespeare_name
FROM UNNEST(top_names) AS name
WHERE name IN (
  SELECT word
  FROM `bigquery-public-data`.samples.shakespeare
);
like image 136
Elliott Brossard Avatar answered Mar 29 '23 08:03

Elliott Brossard


There is currently no way to set/declare variables in BigQuery. If you need variables, you'll need to cut and paste them where you need them. Feel free to file this as a feature request here.

like image 40
Jordan Tigani Avatar answered Mar 29 '23 07:03

Jordan Tigani