[英]Dynamic query using bigquery and data studio
I want to take out data for every date range in Data Studio without the need to change date range selectors in my BigQuery all the time.我想在 Data Studio 中取出每个日期范围的数据,而无需一直更改 BigQuery 中的日期范围选择器。 However, not sure if it is even possible to do so.
但是,不确定是否有可能这样做。 The reasons I do this is to make sure that the queried data is only for 30 days, as later it do some kind of segmentation using that 30 days data.
我这样做的原因是为了确保查询的数据只有 30 天,因为稍后它会使用这 30 天的数据进行某种细分。
Then I figured out that the Data Studio can use dynamic_date, however this way will never produce any datatable (datatable will be used to do other queries from it).然后我发现 Data Studio 可以使用 dynamic_date,但是这种方式永远不会产生任何数据表(数据表将用于从中进行其他查询)。 Is it possible to do dynamic_date in BigQuery instead?
是否可以改为在 BigQuery 中执行 dynamic_date? like retrieving data from BigQuery using a date range not previously defined in the query.
比如使用之前未在查询中定义的日期范围从 BigQuery 检索数据。
From my point of view, code should be like:从我的角度来看,代码应该是这样的:
SELECT
ID,
FROM `table`
WHERE DATE(Timestamp) between $DS_START_DATE and $DS_START_DATE + INTERVAL 30 DAY)
or或者
WHERE DATE(Timestamp) >= @DS_START_DATE
I believe in pure Bigquery you can use DECLARE
clause for that purpose, defining variables of the specified type:我相信在纯 Bigquery 中,您可以为此目的使用
DECLARE
子句,定义指定类型的变量:
declare DS_START_DATE date default "2020-03-03";
declare DS_END_DATE date default "2020-03-04";
WITH sample AS (
SELECT '10001' AS id, cast('2020-03-01' AS timestamp) as date_id UNION ALL
SELECT '10002', cast('2020-03-02' AS timestamp) UNION ALL
SELECT '10003', cast('2020-03-03' AS timestamp) UNION ALL
SELECT '10004', cast('2020-03-04' AS timestamp) UNION ALL
SELECT '10005', cast('2020-03-05' AS timestamp) UNION ALL
SELECT '10006', cast('2020-03-06' AS timestamp)
)
select id, date_id from sample
where date(date_id) between DS_START_DATE and DS_END_DATE
Alternatively, you can take a look at parameterized queries , however as I mentioned in the comment, they are not supported in classic BigQuery web UI.或者,您可以查看参数化查询,但正如我在评论中提到的,经典 BigQuery web UI 不支持它们。
DECLARE
is still not supported when using custom queries in Looker Studio (aka Data Studio), but it's possible to pass a date range into the query like shown below.在 Looker Studio(又名 Data Studio)中使用自定义查询时仍然不支持
DECLARE
,但可以将日期范围传递到查询中,如下所示。 Please note that the dates are passed as strings, hence parse_date
is required.请注意,日期作为字符串传递,因此
parse_date
是必需的。
WHERE DATE(Timestamp) >= parse_date('%Y%m%d', @DS_START_DATE)
There's also a predefined param @DS_END_DATE
when enabling date range.启用日期范围时还有一个预定义的参数
@DS_END_DATE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.