1

想象一下这样的表:

RecordID  Key1  Key2
--------------------
1         111   222  
2         222   111  
3         555   444  
4         000   444  
5         444   000

由于重复项被认为是在列和上具有相同集的记录。Key1Key2

例如,记录:(1 和 2)和(4 和 5)被认为是重复的,因为它们具有相同的条目

因此,重复数据删除的一个示例输出可能是:

1         111   222
3         555   444
4         000   444

任何想法如何在 SQL 中最好地实现这一点?

4

3 回答 3

3

询问:

SQLFIDDL示例

SELECT
t1.*
FROM MyTable t1
  LEFT JOIN MyTable t2
    ON t1.Key1 = t2.Key2
      AND t1.Key2 = t2.Key1
      AND t1.RecordID>t2.RecordID
WHERE t2.RecordID is null

结果:

| RECORDID | KEY1 | KEY2 |
--------------------------
|        1 |  111 |  222 |
|        3 |  555 |  444 |
|        4 |    0 |  444 |
于 2012-11-22T18:18:03.523 回答
-1

这应该得到被欺骗的记录:

SELECT alt.RecordID
FROM yourtable AS main
INNER JOIN yourtable AS alt ON (main.Key1 = alt.Key2) AND (main.Key2 = alt.Key1)
于 2012-11-22T16:45:54.410 回答
-2

要获得您描述的结果,您可以尝试:

SELECT M1.RecordId
FROM MyTable M1
WHERE Key1 IN (
    SELECT Key2 
    FROM MyTable
    WHERE Key2 = M1.Key1 AND RecordID <> M1.RecordId
    )
    AND
    Key2 IN (
    SELECT Key1
    FROM MyTable
    WHERE Key1 = M1.Key2 AND RecordID <> M1.RecordId
    )

要删除记录,只需使用 RecordId ...

就像是 :

DELETE MyTable WHERE RecordId IN ([Previous Query])

要获得重复数据:

SELECT  M1.RecordID
FROM    MyTable AS M1 
        LEFT JOIN MyTable AS MK1 ON M1.Key1 = MK1.Key2 AND M1.RecordID <> MK1.RecordID AND MK1.RecordId > M1.RecordId
        LEFT JOIN MyTable AS MK2 ON M1.Key2 = MK2.Key1 AND M1.RecordID <> MK2.RecordID AND MK2.RecordId > M1.RecordId
WHERE MK2.Key1 IS NOT NULL                   

:) 对不起,误解了你

于 2012-11-22T16:45:24.953 回答