我有一个具有唯一ID但重复行信息的表。
我可以使用此查询找到具有重复项的行
SELECT
PersonAliasId, StartDateTime, GroupId, COUNT(*) as Count
FROM
Attendance
GROUP BY
PersonAliasId, StartDateTime, GroupId
HAVING
COUNT(*) > 1
我可以手动删除行,同时保留此查询所需的1
Delete
From Attendance
Where Id IN(SELECT
Id
FROM
Attendance
Where PersonAliasId = 15
and StartDateTime = '9/24/2017'
and GroupId = 1429
Order By ModifiedDateTIme Desc
Offset 1 Rows)
我不熟悉SQL,足以弄清楚如何使用第一个查询中的行来删除留下最新的重复项。第一个查询返回了超过3481条记录,以便手动逐一执行此操作。
如何像第一个查询一样找到重复的行,并删除除第二个之外的所有行?
答案 0 :(得分:4)
您可以使用Common Table Expression
删除重复项:
WITH Cte AS(
SELECT *,
Rn = ROW_NUMBER() OVER(PARTITION BY PersonAliasId, StartDateTime, GroupId
ORDER BY ModifiedDateTIme DESC)
FROM Attendance
)
DELETE FROM Cte WHERE Rn > 1;
这将保留每个PersonAliasId - StartDateTime - GroupId
组合的最新记录。
答案 1 :(得分:0)
使用MAX聚合函数识别每个组/个人组合的最新startdatetime。然后删除没有最新时间的记录。
DELETE a
FROM attendance as a
INNER JOIN (
SELECT
PersonAliasId, MAX(StartDateTime) AS LatestTime, GroupId,
FROM
Attendance
GROUP BY
PersonAliasId, GroupId
HAVING
COUNT(*) > 1
) as b
on a.personaliasid=b.personaliasid and a.groupid=b.groupid and a.startdatetime < b.latesttime
答案 2 :(得分:0)
与CTE答案相同 - 给Felix支票
delete
from ( SELECT rn = ROW_NUMBER() OVER(PARTITION BY PersonAliasId, StartDateTime, GroupId
ORDER BY ModifiedDateTIme DESC)
FROM Attendance
) tt
where tt.rn > 1