简体   繁体   中英

Delete From Where Exists -- Throwing Error

The query below will select and display all dupes in one of my tables.

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')

So, if I use that as a sub-query in the query below....

Delete
From Table 
Where Exists (sub-query) 

I would expect it to delete all dupes, but instead, I get this message:

Cannot set destination table in jobs with DML statements 

So, my question is, why doesn't the query below delete dupes?

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'))

I don't have any unique IDs in this table.

Have you tried this? Your HAVING clause is filtering on a count(*). You won't find duplicates after you've already done the grouping.

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'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM