2015-04-20 31 views
9

与mysql变量类似的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"; 
+1

我们的[添加为在谷歌张运行BQ查询(https://开头铬。 google.com/webstore/detail/owox-bi-bigquery-reports/fepofngogkjnejgcbdmmkddnpeojbbin)允许您为查询设置参数并重新运行它们,只编辑预设参数的值。 –

回答

6

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

+0

+1如果可用,我会使用增值税。但是,我想如果这些变数是用于日期时间的,我可以提出一个观点,并将这些日期传递给视图条件,对吗? – Kristian

+3

mmh,仍然没有变数,但Datalab与python中的BigQuery查询和变量进行了很酷的交互:https://cloud.google.com/datalab/ –

+0

我们目前使用python变量。 – Kristian

0

如果您使用的R或蟒蛇与吉贝API接口,并且不担心SQL注入,即得。您只是想在查询中迭代交换参数,您可能需要查看mustache模板语言(在R中可用'whisker')。

如果您使用R,您可以使用condusco R软件包进行迭代/自动查询。下面是为完整的R脚本,将完成在查询中同时使用晶须和condusco迭代换出的参数:

library(bigrquery) 
library(condusco) 
library(whisker) 

# create a simple function that will create a query 
# using {{{mustache}}} placeholders for any parameters 

create_results_table <- function(params){ 

    destination_table <- '{{{dataset_id}}}.{{{table_prefix}}}_results_{{{year_low}}}_{{{year_high}}}' 

    query <- ' 
    SELECT * 
    FROM `bigquery-public-data.samples.gsod` 
    WHERE year > {{{year_low}}} 
     AND year <= {{{year_high}}} 
    ' 


    # use whisker to swap out {{{mustache}}} placeholders with parameters 
    query_exec(
    whisker.render(query,params), 
    project=whisker.render('{{{project}}}', params), 
    destination_table = whisker.render(destination_table,params), 
    use_legacy_sql = FALSE 
) 

} 

# create an invocation query to provide sets of parameters to create_results_table 

invocation_query <- ' 
    SELECT 
    "<YOUR PROJECT HERE>" as project, 
    "<YOUR DATASET_ID HERE>" as dataset_id, 
    "<YOUR TABLE PREFIX HERE>" as table_prefix, 
    num as year_low, 
    num+1 as year_high 
    FROM `bigquery-public-data.common_us.num_999999` 
    WHERE num BETWEEN 1992 AND 1995 
' 

# call condusco's run_pipeline_gbq to iteratively run create_results_table over invocation_query's results 

run_pipeline_gbq(
    create_results_table, 
    invocation_query, 
    project = '<YOUR PROJECT HERE>', 
    use_legacy_sql = FALSE 
)