我有一个表,我需要删除重复项。但是,该表的设计使每个客户可以拥有相同的项目。这是一个屏幕截图,例如:
当我尝试删除重复项时,我也会得到两个客户相同的簿号。 绿色区域显示实际重复,但蓝色显示不重复的书籍,因为客户可以借用相同的书籍。
如何仅删除每个客户的行相同位置?绿色区域。
以下是我的代码:如果两个客户的图书编号相同,则无效。
WITH CTE AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY BookNumber ORDER BY BookNumber DESC) AS DUPS
FROM Store.Books
)
SELECT * FROM CTE WHERE DUPS > 1
答案 0 :(得分:1)
以下查询应该为您提供所有带有customerID的重复账簿的列表,您需要做的只是一个简单的删除语句,其中包含删除重复记录的结果
SELECT count(bookNumber), booknumber, customerID FROM TableName GROUP BY booknumber, customerID having count(booknumber)> 1
答案 1 :(得分:1)
WITH CTE AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY BookNumber, Customerid ORDER BY BookNumber) AS DUPS
FROM Store.Books
)
SELECT * FROM CTE WHERE DUPS > 1
-- if you want to delete, replace last line with this:
--DELETE FROM CTE WHERE DUPS > 1
我应该提一下,没有必要通过booknumber desc订购,所以我删除了'desc'部分
答案 2 :(得分:0)
兄弟们,这是找出重复数据的方法之一,试试吧=)
DECLARE @tempTable TABLE(
CustomerID SMALLINT,
BookLoan NVARCHAR(255),
BookNumber INT,
BookAuthor NVARCHAR(255)
)
INSERT INTO @tempTable Values(112,'Clash Of Titans',12345,'Dick VanDyke ')
INSERT INTO @tempTable Values(112,'Clash Of Titans',12345,'Dick VanDyke ')
INSERT INTO @tempTable Values(112,'Clash Of Titans',23457,'Dick VanDyke ')
INSERT INTO @tempTable Values(112,'History of Soda',99899,'Brian Adams ')
Select *,Count(*) 'Occurrance' From @tempTable Group by
CustomerID,BookLoan,BookNumber,BookAuthor having count(*) > 1
Delete from @temptable
where CustomerID = (Select customerID From @tempTable Group by
CustomerID,BookLoan,BookNumber,BookAuthor having count(*) > 1)
AND BookLoan = (Select BookLoan From @tempTable Group by
CustomerID,BookLoan,BookNumber,BookAuthor having count(*) > 1)
AND BookNumber = (Select BookNumber From @tempTable Group by
CustomerID,BookLoan,BookNumber,BookAuthor having count(*) > 1)
AND BookAuthor = (Select BookAuthor From @tempTable Group by
CustomerID,BookLoan,BookNumber,BookAuthor having count(*) > 1)
Select * from @tempTable
或者下面的替代方式
DECLARE @tempTable TABLE(
CustomerID SMALLINT,
BookLoan NVARCHAR(255),
BookNumber INT,
BookAuthor NVARCHAR(255)
)
INSERT INTO @tempTable Values(112,'Clash Of Titans',12345,'Dick VanDyke ')
INSERT INTO @tempTable Values(112,'Clash Of Titans',12345,'Dick VanDyke ')
INSERT INTO @tempTable Values(112,'Clash Of Titans',23457,'Dick VanDyke ')
INSERT INTO @tempTable Values(112,'History of Soda',12345,'Brian Adams ')
;WITH CTE AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY CustomerID, BookLoan,BookNumber,BookAuthor
ORDER BY BookNumber) AS DUPS
FROM @tempTable
)
DELETE FROM @tempTable
WHERE CustomerID = (SELECT CustomerID FROM CTE WHERE DUPS > 1)
AND BookLoan = (SELECT BookLoan FROM CTE WHERE DUPS > 1)
AND BookNumber = (SELECT BookNumber FROM CTE WHERE DUPS > 1)
AND BookAuthor = (SELECT BookAuthor FROM CTE WHERE DUPS > 1)
SELECT * FROM @tempTable