简体   繁体   English

使用 bigquery 和 data studio 进行动态查询

[英]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

More info 更多信息

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

相关问题 BigQuery 使用 Dynamic sql 作为查询的输入源 - BigQuery using Dynamic sql as the input source of a query 将 Google Data Studio 社区连接器与 BigQuery 结合使用时的时间戳查询问题 - Timestamp query issue when using Google Data Studio community connector with BigQuery BigQuery - 使用查询将数据插入分区表 - BigQuery - Insert data into a partitioned table using a query 如何将 Data Studio 控件传递给 BigQuery 自定义查询 - How to pass Data Studio Controls to BigQuery custom query bigquery:查询数据JSON - bigquery: query data JSON 在 Google Data Studio(Looker Studio) 中的现有报告中添加 BigQuery 查询结果 - Adding BigQuery query result in an already existing report in Google Data Studio(Looker Studio) BigQuery 和 Data Studio 使用(成本) - BigQuery and Data Studio usage (cost) Google Workspace、BigQuery 和 Looker/Data Studio - Google Workspace, BigQuery and Looker/Data Studio 如何使用 Data Studio 中的 BigQuery 连接器向参数添加“允许值”列表? - How to add a list of "permitted values" to a parameter by using a BigQuery connector in Data Studio? 在 BigQuery 中使用 HyperLogLog 函数是否可以从对相同数据的相同查询中得到不同的结果? - Using HyperLogLog functions in BigQuery can you get different results from the same query on the same data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM