[英]BigQuery how to query partition by month/year when table partitioned by day?
[英]Given a string value and a timestamp, how to create a custom year based partition table in BigQuery?
我在下面顯示的非分區表“A”中有一些值
{'column_1':'string_1','timestamp':2018-01-01 00:00:00}
{'column_1':'string_6','timestamp':2021-01-01 00:00:00}
{'column_1':'string_2','timestamp':2018-01-01 00:00:00}
{'column_1':'string_4','timestamp':2020-01-01 00:00:00}
{'column_1':'string_3','timestamp':2019-01-01 00:00:00}
{'column_1':'string_5','timestamp':2021-01-01 00:00:00}
如何創建一個新表Table 'B' ,它是按year
分區的,並且在向 BigQuery 中發出插入命令時,新值會自動插入到正確的year
分區中?
表'B'的結構
字符串_1 | 2018-01-01 00:00:00 | 2018年
string_2 | 2018-01-01 00:00:00 | 2018年
string_3 | 2019-01-01 00:00:00 | 2019年
string_4 | 2020-01-01 00:00:00 | 2020年
string_5 | 2021-01-01 00:00:00 | 2021年
string_6 | 2021-01-01 00:00:00 | 2021年
您可以使用 BigQuery 公共數據集表bigquery-public-data:crypto_bitcoin.blocks
使用的技術,它是一個 DAY 分區表,並使用一個月的第一天作為分區列timestamp_month
。
CREATE TEMP TABLE table_a (
column_1 STRING,
timestamp TIMESTAMP,
) as
select "string_1", TIMESTAMP "2018-01-01 00:00:00" UNION ALL
select "string_2", TIMESTAMP "2019-01-01 00:00:00";
CREATE TEMP TABLE table_b (
column_1 STRING,
timestamp TIMESTAMP,
timestamp_year DATE
) PARTITION BY timestamp_year;
INSERT INTO table_b
SELECT column_1,
timestamp,
DATE(EXTRACT(YEAR FROM DATE(timestamp)), 1, 1) timestamp_year
FROM table_a;
SELECT *
FROM table_b;
輸出:
+----------+---------------------+----------------+
| column_1 | timestamp | timestamp_year |
+----------+---------------------+----------------+
| string_1 | 2018-01-01 00:00:00 | 2018-01-01 |
| string_2 | 2019-01-01 00:00:00 | 2019-01-01 |
+----------+---------------------+----------------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.