[英]most effective way to query all the month-end data in BigQuery
I have a table containing the daily transactions with date column.我有一个包含日期列的每日交易的表格。
The table is in BigQuery and is partitioned by the date column.该表位于 BigQuery 中,并按日期列进行分区。
What is the most effective way to query all month-end data from the table?从表中查询所有月末数据的最有效方法是什么?
I tired the sql like below but it processed the whole table which is about 100GB我厌倦了下面的 sql,但它处理了大约 100GB 的整个表
SELECT * FROM table
WHERE date = LAST_DAY(date , month)
It should process less bytes as the table is partitioned by the date?它应该处理更少的字节,因为表是按日期分区的? (like 300 mb if I just choose one specific end of month in the where clause)
(如果我只在 where 子句中选择一个特定的月末,则为 300 mb)
SELECT * FROM table
WHERE date = "2022-11-30"
Any ways to get what I want with processing less data?有什么方法可以通过处理更少的数据来获得我想要的东西吗?
You can use the following query to filter on the last day of the current month and to process only the partition of the last day of month:您可以使用以下查询来过滤当月的最后一天,并仅处理该月最后一天的分区:
SELECT * FROM table
WHERE date = DATE_TRUNC(DATE_ADD(CURRENT_DATE('Europe/Paris'), INTERVAL 1 MONTH), MONTH) - 1;
The same query with a date
column instead of the current date:使用
date
列而不是当前日期的相同查询:
SELECT * FROM table
WHERE date = DATE_TRUNC(DATE_ADD(your_date_column, INTERVAL 1 MONTH), MONTH) - 1;
You can minimize volume of data processed and cost by Calculating a list of In Scope last_date of the month
and apply filter condition over data partitioned tables.您可以通过计算范围内的列表来最小化处理的数据量和成本
In Scope last_date of the month
并对数据分区表应用过滤条件。
Following example will explain you:-以下示例将向您解释:-
Original data looks like as given below, output expected is highlighted record without scanning complete table原始数据如下所示,预期输出是突出显示的记录,无需扫描完整表格
Code to achieve it is:-实现它的代码是:-
with data as
(select '2020-11-20' as add1, 'Robert' as name Union all
select '2021-10-10' as add1, 'Smith' as name Union all
select '2023-9-9' as add1, 'Mike' as name Union all
select '2024-8-2' as add1, 'Donal' as name Union all
select '2025-7-31' as add1, 'Kim' as name ),
-- Calculing Inscope List of last_dates of the month
new_data as
(select add1, LAST_DAY(cast (add1 as date)) as last_dt
from data)
-- Applying filter condition on date fileds
select * from data a, new_data b
where cast (a.add1 as date)=last_dt
Output will be last record which is having last day of the month.输出将是本月最后一天的最后一条记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.