Setting Big Query variables like mysql

前端 未结 5 544
鱼传尺愫
鱼传尺愫 2020-12-28 15:04

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\',

         


        
相关标签:
5条回答
  • 2020-12-28 15:09

    Here is a solution using a user defined function. Declaring variables and calling them looks more like Mysql.

    You can call your variables by using function var("your variable name") this way:

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

    If your variable is not a string, set it as a string, call it with var and safe_cast it to your type:

    #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)
    
    0 讨论(0)
  • 2020-12-28 15:09

    You may want to consider looking into BigQuery's Parameterized Queries. BigQuery supports query parameters to help prevent SQL injection when queries are constructed using user input. This feature is only available with standard SQL syntax.

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

    0 讨论(0)
  • 2020-12-28 15:14

    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
    
    0 讨论(0)
  • 2020-12-28 15:27

    There are no 'variables' to be set in BigQuery, but you could add a feature request: https://code.google.com/p/google-bigquery/issues/list?q=label:Feature-Request

    0 讨论(0)
  • 2020-12-28 15:35

    You can now use variables in BigQuery. To run the statements that you provided, you need to use 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";
    

    You can use variables in statements after declaring them, e.g.:

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

    See also the scripting documentation.

    0 讨论(0)
提交回复
热议问题