下面的查询将选择并显示我的一张表中的所有重复项。
SELECT KeyReport, Analytics, Description, AsOfDate, COUNT(*) as CNT
FROM `corp-analytics.Reports.Metrics`
GROUP BY KeyReport, Analytics, Description, AsOfDate
HAVING COUNT(*) > 1 AND AsOfDate IN('01-03-2019','01-17-2019')
因此,如果我在以下查询中将其用作子查询......
Delete
From Table
Where Exists (sub-query)
我希望它会删除所有重复项,但我会收到此消息:
Cannot set destination table in jobs with DML statements
所以,我的问题是,为什么下面的查询不删除重复对象?
Delete
From `corp-analytics.Reports.Metrics`
Where Exists (SELECT KeyReport, Analytics, Description, AsOfDate, COUNT(*) as CNT
FROM `corp-analytics.Reports.Metrics`
GROUP BY KeyReport, Analytics, Description, AsOfDate
HAVING COUNT(*) > 1 AND AsOfDate IN('01-03-2019','01-17-2019'))
此表中没有任何唯一ID。
答案 0 :(得分:0)
您尝试过吗? 您的HAVING子句正在对count(*)进行过滤。完成分组后,您将找不到重复的内容。
Delete
From `corp-analytics.Reports.Metrics`
Where Exists (SELECT KeyReport, Analytics, Description, AsOfDate, COUNT(*) as CNT
FROM `corp-analytics.Reports.Metrics`
GROUP BY KeyReport, Analytics, Description, AsOfDate
HAVING CNT > 1 AND AsOfDate IN('01-03-2019','01-17-2019'))