在 BigQuery 中对日期进行分组时出现 DATE_ADD 或 DATE_DIFF 错误
DATE_ADD or DATE_DIFF error when grouping dates in BigQuery
尽我所能尝试搜索,我仍然无法弄清楚,尽管 https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#date_add 提供了一些帮助,但我仍然卡住了。我正在尝试将日期分组为数周,但不断收到代码下方的两个错误之一。
day bitcoin_total dash_total
2009-01-03 1 0
2009-01-09 14 0
2009-01-10 61 0
理想的结果是一周的开始日期(可以是星期一或星期日,以哪个为准)
day bitcoin_total dash_total
2008-12-28 1 0
2009-01-04 75 0
这似乎是一个常见问题,但大多数答案都是针对 T-SQL 而不是标准 SQL。我的日期列是 Date
类型,但那是 return 类型,所以这应该不是问题。
DATE_ADD(week, DATE_DIFF(week, 0, day), 0) Date
FROM
my_table
GROUP BY
DATE_ADD(week, DATE_DIFF(week, 0, day), 0)
ORDER BY
DATE_ADD(week, DATE_DIFF(week, 0, day), 0)
如果我将 DATE_ADD 函数中的 date_expression
更改为 DATE "2009-01-01"
,则上述代码或 Error: Expected INTERVAL expression at [2:29]
出现 Unrecognized name: week at [2:10]
错误]
以下适用于 BigQuery 标准 SQL
#standardSQL
SELECT DATE_TRUNC(day, WEEK) AS day,
SUM(bitcoin_total) AS bitcoin_total,
SUM(dash_total) AS dash_total
FROM `project.dataset.table`
GROUP BY day
如果要应用于您问题中的样本数据,如下例所示
#standardSQL
WITH `project.dataset.table` AS (
SELECT DATE '2009-01-03' day, 1 bitcoin_total, 0 dash_total UNION ALL
SELECT '2009-01-09', 14, 0 UNION ALL
SELECT '2009-01-10', 61, 0
)
SELECT DATE_TRUNC(day, WEEK) AS day,
SUM(bitcoin_total) AS bitcoin_total,
SUM(dash_total) AS dash_total
FROM `project.dataset.table`
GROUP BY day
输出将是
Row day bitcoin_total dash_total
1 2008-12-28 1 0
2 2009-01-04 75 0
尽我所能尝试搜索,我仍然无法弄清楚,尽管 https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#date_add 提供了一些帮助,但我仍然卡住了。我正在尝试将日期分组为数周,但不断收到代码下方的两个错误之一。
day bitcoin_total dash_total
2009-01-03 1 0
2009-01-09 14 0
2009-01-10 61 0
理想的结果是一周的开始日期(可以是星期一或星期日,以哪个为准)
day bitcoin_total dash_total
2008-12-28 1 0
2009-01-04 75 0
这似乎是一个常见问题,但大多数答案都是针对 T-SQL 而不是标准 SQL。我的日期列是 Date
类型,但那是 return 类型,所以这应该不是问题。
DATE_ADD(week, DATE_DIFF(week, 0, day), 0) Date
FROM
my_table
GROUP BY
DATE_ADD(week, DATE_DIFF(week, 0, day), 0)
ORDER BY
DATE_ADD(week, DATE_DIFF(week, 0, day), 0)
如果我将 DATE_ADD 函数中的 date_expression
更改为 DATE "2009-01-01"
,则上述代码或 Error: Expected INTERVAL expression at [2:29]
出现 Unrecognized name: week at [2:10]
错误]
以下适用于 BigQuery 标准 SQL
#standardSQL
SELECT DATE_TRUNC(day, WEEK) AS day,
SUM(bitcoin_total) AS bitcoin_total,
SUM(dash_total) AS dash_total
FROM `project.dataset.table`
GROUP BY day
如果要应用于您问题中的样本数据,如下例所示
#standardSQL
WITH `project.dataset.table` AS (
SELECT DATE '2009-01-03' day, 1 bitcoin_total, 0 dash_total UNION ALL
SELECT '2009-01-09', 14, 0 UNION ALL
SELECT '2009-01-10', 61, 0
)
SELECT DATE_TRUNC(day, WEEK) AS day,
SUM(bitcoin_total) AS bitcoin_total,
SUM(dash_total) AS dash_total
FROM `project.dataset.table`
GROUP BY day
输出将是
Row day bitcoin_total dash_total
1 2008-12-28 1 0
2 2009-01-04 75 0