如何在sql server中找到重复的记录。 如果我能找到除最新记录之外的记录,那将是件好事。 因为我想删除除最新记录以外的所有记录。 我计划通过将除最新记录之外的所有记录放入临时表并根据临时表的id删除原始记录来实现此目的
employeeID EmployeeName
1 A
2 B
3 A
4 A
5 B
我想选择1,3(在employeeName ='A'的情况下) 我想在employeeName ='B'
的情况下选择2答案 0 :(得分:1)
试试这个:
重复' A'
的记录SELECT employeeID
FROM Emp AS A
WHERE EmployeeName = 'A' AND
employeeID <> (SELECT MAX(employeeID)
FROM Emp
WHERE EmployeeName = A.EmployeeName)
所有Dulplicates
SELECT employeeID
FROM Emp AS A
WHERE employeeID <> (SELECT MAX(employeeID)
FROM Emp
WHERE EmployeeName = A.EmployeeName)
删除重复记录
DELETE FROM Emp
WHERE employeeID NOT IN (SELECT MAX(employeeID)
FROM Emp
GROUP BY EmployeeName)
答案 1 :(得分:0)
首先将Identity列添加到原始表中 (称为employeeNo)
declare @T table(E_Name nvarchar(255), C int, Max_ID int)
insert @T
select employeeName, count(1), max(employeeNo)
from employees
group by employeeName
having count(1)>1
delete employees
from employees
left join @T on E_Name = employeeName
where C is not null and Max_ID <> employeeNo
答案 2 :(得分:0)
SELECT * FROM Table_1 WHERE employeeID not in
(
SELECT MAX(employeeID)
FROM Table_1
GROUP BY EmployeeName
)
答案 3 :(得分:0)
这将从示例表中选择所有旧的重复项。
SELECT * FROM example WHERE employeeID NOT IN (SELECT MAX(employeeID) _ FROM example)