2

我有一个包含许多列的表,我必须在其中找到基于一列的重复项。

customer_name即如果我在那个Customer_name时候发现重复

  1. 我必须从源表中删除所有重复。
  2. 将所有这些行发送到具有相同结构的其他表。
4

3 回答 3

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)
于 2012-09-06T09:55:46.450 回答
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来展示它是如何工作的。

于 2012-09-06T10:01:00.273 回答
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
于 2012-09-06T10:09:18.923 回答