Bigquery - 使用

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

我有一个表,其中一些记录可能是重复的。

表:dashboard.scrappy.imoveis |数据|莫比利亚里亚 |内戈西奥 |科迪戈 |城市|超滤|蒂波 |上城区 |网址 |终点|总计 |勇气| | --------- | ----------- | -------- | ------ | ------ | ------ | ------ | ------ | ------ | -------- | -----| -----| |时间戳|字符串|字符串|字符串|字符串|字符串|字符串|字符串|字符串|字符串|浮动|浮动|

当 4 列(

data
imobiliaria
negocio
codigo
)中的记录相同时,将识别出重复记录。

表中没有包含行值索引(增量值)的列。

我正在尝试使用以下指令:(删除重复项,留下 1 条记录)

DELETE FROM `dashboard.scrappy.imoveis`
WHERE ((ROW_NUMBER() OVER (PARTITION BY `data`, `imobiliaria`, `negocio`, `codigo`) > 1) AND (COUNT(*) > 1 OR (COUNT(*) = 1 AND ROW_NUMBER() OVER (PARTITION BY `data`, `imobiliaria`, `negocio`, `codigo`) = 1)))
GROUP BY `data`, `imobiliaria`, `negocio`, `codigo`;

但它给出了错误:语法错误:

Syntax error: Expected end of input but got keyword GROUP at [3:1]

关于如何修复它有什么建议吗?

google-bigquery
1个回答
0
投票

您必须重写查询,因为我们不能在删除语句中使用 Group by。聚合/窗口函数也不能在 where 子句中使用。

识别所有列中的重复项的最简单方法是运行

'Select distinct * from table_name'

这将为您提供独特的行。 您可以将此结果临时存储在不同的表中或替换现有的表:

create or replace table table_name as
select distinct * from table_name.

或者您可以使用 with 语句并删除多余的行:

with cte as
(select *,
ROW_NUMBER() OVER (PARTITION BY `data`, `imobiliaria`, `negocio`, `codigo`) as dup_row from table_name )
delete from cte where dup_row > 1
© www.soinside.com 2019 - 2024. All rights reserved.