简体   繁体   English

在计划查询 BigQuery 中声明变量;

[英]Declare variables in scheduled query BigQuery;

I am developing a scheduled query where I am using the WITH statement to join and filtrate several tables from BigQuery.我正在开发一个计划查询,我在其中使用 WITH 语句连接和过滤 BigQuery 中的多个表。 To filtrate the dates, I would like to declare the following variables: DECLARE initial, final DATE;为了过滤日期,我想声明以下变量:DECLARE initial, final DATE;

SET initial = DATE_TRUNC(DATE_TRUNC(CURRENT_DATE(), MONTH)+7,ISOWEEK);
SET final = LAST_DAY(DATE_TRUNC(CURRENT_DATE(), MONTH)+7, ISOWEEK);

However, when executing this query, I am getting two results;但是,在执行此查询时,我得到了两个结果; one for the variables declared (which I am not interested in having them as output), and the WITH statement that is selected at the end (which as the results that I am interested in).一个用于声明的变量(我对将它们作为输出不感兴趣),以及最后选择的 WITH 语句(这是我感兴趣的结果)。

The principal problem is that, whenever I try t connect this scheduled query to a table in Google Data Studio I get the following error:主要问题是,每当我尝试将此计划查询连接到 Google Data Studio 中的表时,我都会收到以下错误:

Invalid value: configuration.query.destinationTable cannot be set for scripts;无效值:无法为脚本设置configuration.query.destinationTable;

How can I declare a variable without getting it as a result at the end?我怎样才能声明一个变量而不最终得到它呢?

Here you have a sample of the code I am trying work in:这里有我正在尝试使用的代码示例:

DECLARE initial, final DATE;
SET initial = DATE_TRUNC(DATE_TRUNC(CURRENT_DATE(), MONTH)+7,ISOWEEK);
SET final = LAST_DAY(DATE_TRUNC(CURRENT_DATE(), MONTH)+7, ISOWEEK);
WITH HelloWorld AS (

SELECT shop_date, revenue
FROM fulltable
WHERE shop_date >= initial
  AND shop_date <= final

)
SELECT * from HelloWorld;

with initial1 as ( select DATE_TRUNC(DATE_TRUNC(CURRENT_DATE(), MONTH)+7,ISOWEEK) as initial2),

final1 as ( select LAST_DAY(DATE_TRUNC(CURRENT_DATE(), MONTH)+7, ISOWEEK) as final2),

HelloWorld AS (
SELECT shop_date, revenue
FROM fulltable
WHERE shop_date >= (select initial2 from initial1) AND shop_date <= (select final2 from final1)
)

SELECT * from HelloWorld;

With config table having just 1 row and cross-joining it with your table, your query can be written like below.使用只有 1 行的配置表并将其与您的表交叉连接,您的查询可以如下所示编写。

WITH config AS (
  SELECT DATE_TRUNC(DATE_TRUNC(CURRENT_DATE(), MONTH)+7,ISOWEEK) AS initial,
         LAST_DAY(DATE_TRUNC(CURRENT_DATE(), MONTH)+7, ISOWEEK) AS final
),
HelloWorld AS (
  SELECT * FROM UNNEST([DATE '2022-06-06']) shop_date, config
   WHERE shop_date >= config.initial AND shop_date <= config.final
)
SELECT * FROM HelloWorld;

A few patterns I've used:我用过的几种模式:

  1. If you have many that have the same return type (STRING)如果您有许多具有相同返回类型 (STRING)
CREATE TEMP FUNCTION config(key STRING)
RETURNS STRING AS (
  CASE key
    WHEN "timezone" THEN "America/Edmonton"
    WHEN "something" THEN "Value"
  END
);

Then use config(key) to retrieve the value.然后使用 config(key) 检索值。

Or,或者,

  1. Create a function for each constant为每个常量创建一个 function
CREATE TEMP FUNCTION timezone()
RETURNS STRING AS ("America/Edmonton");

Then use timezone() to get the value.然后使用 timezone() 获取值。

It would execute the function each time, so don't do something expensive in there (like SELECT from another table).它每次都会执行 function,所以不要在那里做一些昂贵的事情(比如来自另一个表的 SELECT)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM