我有一个包含许多列的表,我必须根据一列找到副本。
即。如果我在customer_name
中找到重复的Customer_name
答案 0 :(得分:2)
我猜每行都有一个唯一的Id主键。
这将插入您的重复行表:
Insert into duplicateRowsTable
select * from myTable t1
where (select count(*) from myTable t2 where t1.customerId = t2.customerId) > 1
您从duplicateRowsTable中删除了好行:
delete from duplicatesTable
where --this is not the faulty row for each customerId
最后你从第一张桌子上删除了:
delete from myTable
where id IN (select id from duplicatesTable)
答案 1 :(得分:2)
如果您有两个这样的表:
CREATE TABLE t1 (ID int, customerName varchar(64))
CREATE TABLE t2 (ID int, customerName varchar(64))
你可以做这样的事情:( ID列只是为了设置一个基础来保存,你可以根据需要改变它)
--First Copy
WITH CTE_T1
AS
(
SELECT
ID,
customerName,
ROW_NUMBER() OVER(PARTITION BY customerName ORDER BY ID) as OrderOfCustomer
FROM
t1
)
INSERT INTO t2
SELECT ID, customerName FROM cte_T1
WHERE OrderOfCustomer > 1;
--Then Delete
WITH CTE_T1
AS
(
SELECT
ID,
customerName,
ROW_NUMBER() OVER(PARTITION BY customerName ORDER BY ID) as OrderOfCustomer
FROM
t1
)
DELETE FROM CTE_T1
WHERE OrderOfCustomer > 1
这是一个SQLFiddle来展示它是如何运作的。
答案 2 :(得分:2)
试试这个:
用于移动重复项
INSERT Into DuplicatesTable
SELECT *
FROM
(SELECT *, ROW_NUMBER() OVER(PARTITION BY Customer_name ORDER BY Customer_name) As RowID,
FROM SourceTable) as temp
WHERE RowID > 1
用于侦测:
WITH TableCTE
AS
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY Customer_name ORDER BY Customer_name) AS RowID
FROM SourceTable
)
DELETE
FROM TableCTE
WHERE RowID> 1