【发布时间】:2017-06-18 22:26:36
【问题描述】:
我有一个包含大量重复项的数据库,每个都有一个唯一的ID,但它们的PermitID 和EncID 是相同的。我需要删除数据库中除最高 ID 之外的所有内容。
sql语句,
DELETE FROM tblInvoices
WHERE EncID = '0237' AND PermitID IN (
SELECT Max(ID) FROM tblInvoices Group BY PermitID)
删除所有记录。我试过了
DELETE FROM tblInvoices
WHERE EncID = '0237' AND PermitID
< (SELECT Max(ID) FROM tblInvoices Group BY PermitID)
但我收到错误
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
数据的一个例子是
ID PermitID EncID
1 11 22
2 11 22
3 11 22
4 12 23
5 12 23
我想保留 3,删除 2 和 1。我还想保留 5 并删除 4
【问题讨论】:
标签: sql-server sql-server-2008 sql-delete