我正在验证具有电子商务网站的交易级别数据的表,并找到确切的错误。

我希望您的帮助在SQL Server的50列表中找到重复的记录。

假设我的数据是:

OrderNo shoppername amountpayed city Item
1       Sam         10          A    Iphone
1       Sam         10          A    Iphone--->>Duplication to be detected
1       Sam         5           A    Ipod
2       John        20          B    Macbook
3       John        25          B    Macbookair
4       Jack        5           A    Ipod

假设我使用以下查询:
Select shoppername,count(*) as cnt
from dbo.sales
having count(*) > 1
group by shoppername

会返回我
Sam  2
John 2

但是我不想在1或2列以上找到重复项。我想在数据中的所有列上找到重复项。我想要的结果是:
1       Sam         10          A    Iphone

最佳答案

with x as   (select  *,rn = row_number()
            over(PARTITION BY OrderNo,item  order by OrderNo)
            from    #temp1)

select * from x
where rn > 1

您可以通过替换select语句来删除重复项
delete x where rn > 1

关于sql - 使用SQL Server在表中查找重复记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9849846/

10-10 00:56
查看更多