设置像Bigql这样的Big Query变量

时间:2015-04-20 22:34:16

标签: mysql google-bigquery

与mysql变量等效的bigquery是什么?

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";

5 个答案:

答案 0 :(得分:18)

您可以使用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)

或更罗word:

#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

答案 1 :(得分:7)

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

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";

另请参阅scripting documentation

答案 2 :(得分:6)

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

答案 3 :(得分:1)

您可能需要考虑查看BigQuery的参数化查询。当使用用户输入构造查询时,BigQuery支持查询参数以帮助防止SQL注入。此功能仅适用于标准SQL语法。

https://cloud.google.com/bigquery/docs/parameterized-queries

答案 4 :(得分:0)

这是使用Angular nomod error even when the module declared with empty array的解决方案。声明变量并调用它们看起来更像Mysql。

您可以通过以下方式使用函数var("your variable name")来调用变量:

#standardSQL
-- Set your variables here
CREATE TEMP FUNCTION var(str STRING)
RETURNS STRING
LANGUAGE js AS """
  var result = {
    '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',

    'minimumamount': '3.50',

    'default': 'undefined'
  };
  return result[str] || result['default'];
""";
-- Then use them by using the function var("your variable name")
SELECT *
FROM your_table
WHERE date_column BETWEEN var("fromdate") AND var("todate")

如果您的变量不是字符串,请将其设置为字符串,使用var对其进行调用,并将其安全地广播到您的类型:

#standardSQL

CREATE TEMP FUNCTION var(str STRING)
RETURNS STRING
LANGUAGE js AS """
  var result = {
    'minimumamount': '3.50',
    'default': 'undefined'
  };
  return result[str] || result['default'];
""";

SELECT *
FROM your_table
WHERE amount > safe_cast(var("minimumamount") AS FLOAT64)