Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Big Query variables like mysql

what is the bigquery equivalent to mysql variables like?

SET @fromdate = '2014-01-01 00:00:00',  -- dates for after 2013 @todate='2015-01-01 00:00:00',  @bfromdate = '2005-01-01 00:00:00', -- dates for before 2013 @btodate = '2005-01-01 00:00:00',  @achfromdate  = '2013-01-01 00:00:00', -- dates for ACH without submit time in 2013 @achtodate  = '2013-01-01 00:00:00',  @currency="USD"; 
like image 422
Chris Hansen Avatar asked Apr 20 '15 22:04

Chris Hansen


People also ask

How do you set a variable in a large query?

SET x = 5; The following example sets the variable a to have the value 4, b to have the value 'foo', and the variable c to have the value false . SET (a, b, c) = (1 + 3, 'foo', false); The following example assigns the result of a query to multiple variables.

Is BigQuery similar to MySQL?

Google BigQuery and MySQL are primarily classified as "Big Data as a Service" and "Databases" tools respectively. "High Performance" is the primary reason why developers consider Google BigQuery over the competitors, whereas "Sql" was stated as the key factor in picking MySQL.

Can I use BigQuery as database?

BigQuery stores data in a columnar format, achieving a high compression ratio and scan throughput. However, you can also use BigQuery with data stored in other Google Cloud services such as BigTable, Cloud Storage, Cloud SQL and Google Drive.


1 Answers

You could use a WITH clause. It's not ideal, but it gets the job done.

-- Set your variables here WITH vars AS (   SELECT '2018-01-01' as from_date,          '2018-05-01' as to_date )  -- Then use them by pulling from vars with a SELECT clause SELECT * FROM   your_table  WHERE  date_column BETWEEN           CAST((SELECT from_date FROM vars) as date)           AND           CAST((SELECT to_date FROM vars) as date) 

Or even less wordy:

#standardSQL -- Set your variables here WITH vars AS (   SELECT DATE '2018-01-01' as from_date,          DATE '2018-05-01' as to_date ) -- Then use them by pulling from vars with a SELECT clause SELECT * FROM your_table, vars  WHERE date_column BETWEEN from_date AND to_date 
like image 174
user1699188 Avatar answered Oct 04 '22 08:10

user1699188