设置像Bigql这样的Big Query变量

Lou*_*ost 20 mysql google-bigquery

什么是bigquery等同于mysql变量?

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";
Run Code Online (Sandbox Code Playgroud)

小智 28

您可以使用WITH子句.这不是理想的,但它完成了工作.

-- 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)
Run Code Online (Sandbox Code Playgroud)

甚至不那么罗嗦:

#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
Run Code Online (Sandbox Code Playgroud)


Fel*_*ffa 6

BigQuery中没有要设置的"变量",但您可以添加功能请求:https://code.google.com/p/google-bigquery/issues/list? q = label:功能 - 请求

  • mmh,仍然没有变量,但Datalab与python中的BigQuery查询和变量有很酷的互动:https://cloud.google.com/datalab/ (3认同)

Ell*_*ard 6

您现在可以在BigQuery中使用变量。要运行您提供的语句,您需要使用DECLARE

DECLARE fromdate TIMESTAMP DEFAULT '2014-01-01 00:00:00';  -- dates for after 2013
DECLARE todate TIMESTAMP DEFAULT '2015-01-01 00:00:00';

DECLARE bfromdate TIMESTAMP DEFAULT '2005-01-01 00:00:00'; -- dates for before 2013
DECLARE btodate TIMESTAMP DEFAULT '2005-01-01 00:00:00';

DECLARE achfromdate TIMESTAMP DEFAULT '2013-01-01 00:00:00'; -- dates for ACH without submit time in 2013
DECLARE achtodate TIMESTAMP DEFAULT '2013-01-01 00:00:00';

DECLARE currency STRING DEFAULT "USD";
Run Code Online (Sandbox Code Playgroud)

您可以在声明变量后在语句中使用变量,例如:

DECLARE fromdate TIMESTAMP DEFAULT '2014-01-01 00:00:00';  -- dates for after 2013
DECLARE todate TIMESTAMP DEFAULT '2015-01-01 00:00:00';

SELECT FORMAT('From %t to %t', fromdate, todate);
Run Code Online (Sandbox Code Playgroud)

另请参见脚本编制文档