简体   繁体   English

如何在 Data Studio BigQuery 社区连接器的 SQL 查询中包含日期范围

[英]How to include date range in SQL query for Data Studio BigQuery community connector

I'm trying to make a community connector using the advanced services from Google's Data Studio to connect to my BigQuery data table.我正在尝试使用 Google 数据工作室的高级服务制作社区连接器,以连接到我的 BigQuery 数据表。 The connector is all set up and my getData function returns a query which looks like:连接器已全部设置好,我的 getData 函数返回一个查询,如下所示:

var sqlString = "SELECT * FROM `PROJECT.DATASET.TABLE` WHERE " +
"DATE(timestamp) >= @startDate AND DATE(timestamp) <= @endDate;"

where PROJECT, DATASET, and TABLE are filled in with their respective IDs.其中 PROJECT、DATASET 和 TABLE 用它们各自的 ID 填充。 The 'timestamp' field is a BigQuery field in my data table of type TIMESTAMP. 'timestamp' 字段是我的 TIMESTAMP 类型数据表中的 BigQuery 字段。

In my getConfig function, I'm setting the configuration to add a daterange object to the request passed into getData:在我的 getConfig 函数中,我正在设置配置以将 daterange 对象添加到传递给 getData 的请求中:

function getConfig() {
  ...
  config.setDateRangeRequired(true);
  ...
}

I'm then returning the community connector object (defined as 'cc' variable in code below) in my getData function, setting the sql string, query parameters for startDate and endDate, and some other necessary info:然后,我将在我的 getData 函数中返回社区连接器对象(在下面的代码中定义为“cc”变量),设置 sql 字符串、startDate 和 endDate 的查询参数以及其他一些必要信息:

function getData(request) {
  ...
  return cc
    .newBigQueryConfig()
    .setAccessToken(accessToken) // defined earlier
    .setBillingProjectId(billingProjectId) // defined earlier
    .setUseStandardSql(true)
    .setQuery(sqlString)
    .addQueryParameter('startDate', bqTypes.STRING, 
  request.dateRange.startDate)
    .addQueryParameter('endDate', bqTypes.STRING, 
  request.dateRange.endDate)
}

When I run this connector in a report, it connects to BigQuery and even queries the table, but it does not return any data.当我在报告中运行此连接器时,它连接到 BigQuery 甚至查询表,但它不返回任何数据。 When I replace @startDate and @endDate with string literals of format 'yyyy-mm-dd', it works as expected, so it seems like my only problem is that I can't figure out how to set the date range parameters in the query (which I assume I'm supposed to do to allow date range control in data studio reports).当我用格式为 'yyyy-mm-dd' 的字符串文本替换 @startDate 和 @endDate 时,它​​按预期工作,所以似乎我唯一的问题是我无法弄清楚如何在查询(我假设我应该这样做以允许数据工作室报告中的日期范围控制)。 How do I configure this daterange object so that people can control daterange tags in data studio reports?如何配置此日期范围对象,以便人们可以控制数据工作室报告中的日期范围标签?

Edit: For clarification, I know how to add the date range control on a report.编辑:为了澄清起见,我知道如何在报告中添加日期范围控件。 The problem is that the query does not return any data even when the date range query parameters are passed in.问题是即使传入了日期范围查询参数,查询也不会返回任何数据。

I ended up fixing my SQL query.我最终修复了我的 SQL 查询。 I made my WHERE condition as我把我的 WHERE 条件设置为

WHERE DATE(requestTimestamp) BETWEEN @startDate AND @endDate

and it actually returned data correctly.它实际上正确地返回了数据。 I didn't mention another parameter I was using in my query because I thought it was irrelevant, but I had quotes around another conditioned parameter, which may have screwed up the query.我没有提到我在查询中使用的另一个参数,因为我认为它无关紧要,但是我在另一个条件参数周围加上了引号,这可能会搞砸查询。 The condition before was more like:之前的情况更像是:

WHERE id = '@id' AND DATE(requestTimestamp) BETWEEN @startDate AND @endDate

I think putting quotes around @id was the problem, because changing the query to:我认为在 @id 周围加上引号是问题所在,因为将查询更改为:

WHERE id = @id AND DATE(requestTimestamp) BETWEEN @startDate AND @endDate

worked perfectly完美地工作

You can use a Date range control and configured the timestamp field to it.您可以使用日期范围控件并为其配置时间戳字段。 It should automatically pick the timestamp type field.它应该自动选择时间戳类型字段。

Go to Insert and select Date range control to add it to your report.转到Insert并选择Date range control以将其添加到您的报告中。

在此处输入图片说明

You can select the date range in view mode.您可以在查看模式中选择日期范围。

在此处输入图片说明

Like this,像这样,

在此处输入图片说明

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

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