SELECT
*
FROM
Registrations AS R1
INNER JOIN Registrations AS R2
ON R2.UserItemId = R1.UserItemId
AND R2.CourseOfferingId = R1.CourseOfferingId
AND R2.Id != R1.Id
WHERE
R2.Id > R1.Id
我应该如何删除重复的值?
由于
答案 0 :(得分:0)
您可以尝试这样的查询
;with cte as (
select *, RowN = row_number() over(partition by useritemid, courseofferingid, id order by id) from Registrations )
delete from cte where Rown > 1
答案 1 :(得分:0)
我会抓住你的问题。根据您提供的查询,这只是猜测。一个完整的问题将描述重复行的确切含义。
delete from Registrations
where exists (
select 1
from Registrations r2
where r2.UserItemId = Registrations.UserItemId
and r2.CourseOfferingId = Registrations.CourseOfferingId
and r2.Id < Registrations.Id
);
显然,您有多个行具有相同的UserItemId
和CourseOffereningId
但不同的Id
值。我假设您要保留最低Id
的那个并丢弃其他人。
查询中的子查询占用表中的每一行,并检查是否有另一行像这样但具有较低的Id。如果答案是肯定的话则删除该行。