【问题标题】:Setting Big Query variables like mysql设置大查询变量,如 mysql
【发布时间】:2015-06-27 20:55:11
【问题描述】:

什么是等价于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";

【问题讨论】:

标签: mysql google-bigquery


【解决方案1】:

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

甚至更少罗嗦:

#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

【讨论】:

  • 这就是我要找的!
  • 这是一个很棒的解决方案。我正在使用带有 BigQuery 的 DataStudio,这解决了查询中不能有多个句子的问题,否则会出错。
【解决方案2】:

您现在可以在 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";

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

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

另请参阅scripting documentation

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • +1 如果 var 可用,我也会使用它们。但是,我想如果变量是用于日期时间的,我可以创建一个视图,并将日期传递给视图条件,不是吗?
    • mmh,仍然没有 vars,但 Datalab 与 Python 中的 BigQuery 查询和变量进行了很酷的交互:cloud.google.com/datalab
    • 我们目前使用python vars。
    • 目前你可以声明变量,所以这个答案不再是最新的(但当时是一个很好的建议)
    【解决方案4】:

    这是使用a user defined function 的解决方案。声明变量并调用它们看起来更像 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 调用它并将其 safe_cast 为您的类型:

    #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)
    

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-22
        • 2020-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-06
        • 2021-02-11
        相关资源
        最近更新 更多