仅DML的BigQuery中删除重复记录

问题描述 投票:0回答:0
DDLs

CREATE table)。因此,我只需要使用

DML
语句来删除重复项。
id | fname  | age|  dt
1  | 'John' | 23 | '2024-10-16'
1  | 'John' | 31 | '2024-12-23'
1  | 'John' | 29 | '2025-02-08'
2  | 'Tony' | 25 | '2024-11-24'
2  | 'Tony' | 34 | '2025-01-06'
3  | 'Peter'| 42 | '2024-10-17'
表明:

id | fname | age| dt 1 | 'John' | 29 | '2025-02-08' 2 | 'Tony' | 34 | '2025-01-06' 3 | 'Peter'| 42 | '2024-10-17'
根据最新的
[id + fname]

我提出的编码使用了创建语句:
[dt]

我无法执行上述语句,因为我没有prod中的DDL访问。如何仅使用
create or replace table `prod.emp_dedup` as select * from `prod.emp` qualify row_number() over (partition by id, fname order by dt desc) = 1; truncate table `prod.emp`; insert into `prod.emp` select * from `prod.emp_dedup`; drop table `prod.emp_dedup`;
(DML)语句来实现这一目标?
thanks.

您可以通过利用公共表表达式(CTE)来识别重复项,然后删除它们。

DELETE
cte“最新_records”将行号(`rn`)分配给``iD'和fname'的每个组中的每一行,并按dt'dt'顺序排序。带有`rn = 1`的行是每个组的最新记录。删除语句删除了`prod.emp`的所有行,这不是其各自组的最新记录

希望它给你一些指针。

	

sql google-bigquery
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.