[英]MS Access FInd Duplicates
I have a MS Access table that had duplicate entries loaded to it.我有一个 MS Access 表,其中加载了重复的条目。 I used the query wizard and it returns both records.
我使用了查询向导,它返回了两条记录。 I need to return one record preferably with the id so I can use it to delete only the duplicate.
我需要最好返回一条带有 id 的记录,这样我就可以用它来只删除重复的记录。 Its been awhile since I used MS Access can this be done?
自从我使用 MS Access 以来已经有一段时间了,可以这样做吗?
UpdateTable
ID
PkgId
CompName
UpdDate
UpdQty
Data:数据:
7212797 ADJ E5780 9/27/2019 7;
7213166 ADJ E5780 9/27/2019 7;
7212708 ADJ E5912 9/27/2019 7;
7213167 ADJ E5912 9/27/2019 7;
Consider the approach using a correlated subquery with an EXISTS
condition:考虑使用具有
EXISTS
条件的相关子查询的方法:
SELECT id
FROM mytable t
WHERE EXISTS (
SELECT 1
FROM mytable t1
WHERE
t1.PkgId = t.PkgId
AND t1.CompName = t.CompName
AND t1.UpdDate = t.UpdDate
AND t1.UpdQty = t.UpdQty
AND t1.id < t.id
)
This will return all id
for which a duplicate exists (ie same pkgId/CompName/UpdDate/UpdQty
).这将返回存在重复的所有
id
(即相同pkgId/CompName/UpdDate/UpdQty
)。 The record with the lowest id
is considered the original record and will not be returned by the query. id
最低的记录被认为是原始记录,不会被查询返回。
If you want to delete the records, there are multiple ways.如果要删除记录,有多种方法。 I would go for a comparison on the
id
column:我会 go 对
id
列进行比较:
delete from t
where t.id < (select max(t2.id)
from t as t2
where t2.compname = t.compname and
t2.upddate = t.update and
t2.updqty = t.updqty and
);
That said, this will not work if any of the comparison columns are null
.也就是说,如果任何比较列是
null
,这将不起作用。 If id
is a primary key, you can instead use:如果
id
是主键,您可以改为使用:
delete from t
where t.id not in (select max(t2.id)
from t t2
group by pkgid, compname, upddate
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.