简体   繁体   中英

BigQuery how to query partition by month/year when table partitioned by day?

I partitioned a table using the following command on the date field and it got partitioned by day by default.

CREATE TABLE `sales.sales_txn_partition` PARTITION BY DateOfTxn CLUSTER BY SalesPersonID AS (SELECT * FROM `sales.sales_records` WHERE SalesDate IS NOT NULL)

在此处输入图像描述

Now, I want to filter based on year/month as well in my WHERE clause instead of writing the complete date. Is it possible or I'd need to create new partitioned table for year/month separately?

I'm using the following command, nothing works

SELECT * FROM `sales.sales_txn_partition` WHERE DATE_TRUNC(EXTRACT(DATE FROM _PARTITIONTIME), MONTH) = 2018; 
## Doesnt work: Query error: Unrecognized name: _PARTITIONTIME at [3:78]

SELECT * FROM `sales.sales_txn_partition` WHERE EXTRACT(YEAR from DateOfTxn)=2018;
## Works but scans the full table

Why not just use direct comparisons?

where DateOfTxn >= date('2018-01-01') and
      DateOfTxn < date('2019-01-01')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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