Google bigquery 在时间戳字段上按天分区的查询BigQuery表
我有一个按天在时间戳字段上划分的BigQuery表,如下所示: 数据样本:Google bigquery 在时间戳字段上按天分区的查询BigQuery表,google-bigquery,Google Bigquery,我有一个按天在时间戳字段上划分的BigQuery表,如下所示: 数据样本: Row _time dummy_column 1 2020-06-15 23:57:00 UTC a 2 2020-06-15 23:58:00 UTC b 3 2020-06-15 23:59:00 UTC c 4 2020-06-16 00:00:00 UTC d 5 2020-06-16 00:00:01 UTC e 6 2020-06-16
Row _time dummy_column
1 2020-06-15 23:57:00 UTC a
2 2020-06-15 23:58:00 UTC b
3 2020-06-15 23:59:00 UTC c
4 2020-06-16 00:00:00 UTC d
5 2020-06-16 00:00:01 UTC e
6 2020-06-16 00:00:02 UTC f
由于表是在\u time
上分区的,但它是按天分区的,因此为了在特定的天分区2020-06-15
中查询,我运行:
select * from {DATASET}.{TABLE} where _time >= TIMESTAMP("2020-06-15") and _time < TIMESTAMP("2020-06-16");
我的问题是:有没有一种直接查询日分区的方法,即显式地提及它而不是使用时间戳范围进行查询?当您有一个按日分区的表时,您可以直接引用要查询的分区日 为了演示您的案例,我使用了下表模式:
Field name Type Mode Policy tags Description
date_formatted DATE NULLABLE
fullvisitorId STRING NULLABLE
其他表格的详细信息
Table type Partitioned
Partitioned by Day
Partitioned on field date_formatted
Partition filter Not required
还有一些样本数据
Row date_formatted fullvisitorId
1 2016-12-30 6449885916997461186
2 2016-12-30 3401232735815769402
3 2016-12-30 2100622457042859506
4 2016-12-30 4434434796889840043
5 2016-12-31 9382207991125014696
6 2017-12-30 4226029488400478200
7 2017-12-31 4304624161918005939
8 2017-12-31 4239590118714521081
9 2018-12-30 0030006068136142781
10 2018-12-30 7849866399135936504
您可以使用下面的语法查询上述示例数据
DECLARE dt DATE DEFAULT Date(2016,12,30);
SELECT * FROM `project.dataset.table_name` WHERE date_formatted = dt
输出
Row date_formatted fullvisitorId
1 2016-12-30 6449885916997461186
2 2016-12-30 3401232735815769402
3 2016-12-30 2100622457042859506
4 2016-12-30 4434434796889840043
正如您所见,它只检索了我声明的特定日期的数据
请注意,我之所以使用该子句,是因为它有助于修改日期过滤器。此外,如果字段格式为时间戳,则可以替换为在变量中定义过滤器
作为附加信息,如果您想使用一个范围,请考虑使用诸如子句之间的子句,如DATEY1和DATEY2 更新:
这次我使用了您的示例数据,我使用下面的语法创建了一个与您描述的完全相同的表。代码如下:create table dataset.table_name(_time timestamp, dummy_column string) partition by date(_time)
as select timestamp '2020-06-15 23:57:00 UTC' as _time, "a" as dummy_column union all
select timestamp '2020-06-15 23:58:00 UTC' as _time, "b" as dummy_column union all
select timestamp '2020-06-15 23:59:00 UTC' as _time, "c" as dummy_column union all
select timestamp '2020-06-16 00:00:00 UTC' as _time, "d" as dummy_column union all
select timestamp '2020-06-16 00:00:01 UTC' as _time, "e" as dummy_column union all
select timestamp '2020-06-16 00:00:02 UTC' as _time, "f" as dummy_column
下表:
模式:
详情如下:
要从时间戳字段(_time)中仅选择一个日期,可以执行以下操作:
SELECT * FROM `project.dataset.table` WHERE DATE(_time) = "2020-06-15"
以及产量,
Row date_formatted fullvisitorId
1 2016-12-30 6449885916997461186
2 2016-12-30 3401232735815769402
3 2016-12-30 2100622457042859506
4 2016-12-30 4434434796889840043
如上所示,输出符合您的要求
此外,作为一个额外信息,我想鼓励您查看此文档。谢谢@Alexandre,但您的回答与我在问题中提到的问题一致,无论中间条款如何。是的,我的分区字段是timestamp,我想查询一天分区,所以我的问题是,如果有一种方法可以在整天的单个分区上查询该分区,而不是使用时间戳范围进行查询?@YasserMohsen,您可以按照我的查询中的方式来做,声明您想要的时间戳,并在where子句中用“=”号将其写入。我在上面发布的查询只返回2016/12/30日的结果。我已经用一个输出更新了答案。首先,忘记“Declare”子句,使用与否没有区别,只是为了让查询更漂亮。关于像您的查询那样直接使用日期,它在我的情况下是无效的,因为我有时间戳而不是日期。请看一下我问题中的数据样本。谢谢。最后一个查询正是我想要的。请考虑一下我的问题是否更清楚。“YasserMohsen,我已经投票赞成了这个问题。我很高兴能帮上忙。