简体   繁体   English

在BigQuery中查询所有月末数据的最有效方法

[英]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.

相关问题 如何在 BigQuery 中加载 shapefile? 有没有更简单的方法在 BigQuery 中上传多边形数据? - How to load a shapefile in BigQuery? Is there a simpler way to upload polygon data in BigQuery? 在 DynamoDB 中更新批量数据而不是扫描和查询的最有效方法是什么 - what is the most efficient way to update bulk data inside DynamoDB rather than scan and query 如何在大多数查询处于年月级别的日期字段上优化 BigQuery 查询 - How to optimize BigQuery queries on a date field where most queries are at a year-month level 有没有办法在 BigQuery 表中查看查询或上次历史记录? - Is there a way to view the query or last history in a BigQuery Table? Bigquery - UNION ALL 具有不同参数的相同查询 - Bigquery - UNION ALL same query with different parameters 按时间戳月份查询firebase条数据 - Query firebase data by timestamp month 如何使用 Union All function 指定 BigQuery 中显示的数据顺序 - How to specify the order of data displayed in BigQuery using the Union All function Bigquery 在流式传输时无法加载所有数据 - Bigquery cannot load all data when streaming 如何使用 bigquery 从本月至今的所有卖家中获得前 1000 名卖家的交易金额百分比 - how to get % of transaction amount from top 1000 sellers over all sellers, Month to Date, using bigquery 如何查询firebase实时数据库获取上月数据 - How to query firebase realtime database to get last month data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM