我试图通过在两列下找到重复项来删除表中的记录。
我已设法使用以下内容识别记录;
SELECT SiteName, Postcode
FROM [Active Site Locations]
GROUP BY SiteName, Postcode
HAVING count(*) > 1;
但是,当我尝试运行DELETE将其从表中删除时,我得到以下内容。
DELETE FROM [Active Site Locations]
WHERE (Select SiteName, Postcode, count(*) FROM [Active Site Locations]
GROUP BY SiteName, Postcode
HAVING count(*) >1 );
您有一个子查询可以返回多个字段而不使用主查询的from子句中的EXISTS保留字。修改子查询的select语句以仅请求一个字段。
但我需要检查SiteName和Postcode列,如果匹配,那么我需要删除它们,请有人帮忙。
答案 0 :(得分:1)
这是一个方法,它使用相关子查询来计算匹配数,然后在外部查询中使用> 1
进行比较:
DELETE FROM [Active Site Locations]
WHERE (SELECT COUNT(*)
FROM [Active Site Locations] as asl
WHERE asl.SiteName = [Active Site Locations].SIteName AND
asl.Postcode = [Active Site Locations].Postcode
) > 1;