SQL将重复项删除为多列的集合

时间:2012-11-22 16:25:29

标签: sql

想象一下这样的表:

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

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

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

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

1         111   222
3         555   444
4         000   444

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

3 个答案:

答案 0 :(得分:3)

查询:

<强> SQLFIDDLExample

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 |

答案 1 :(得分:-1)

这应该得到dupe'd记录:

SELECT alt.RecordID
FROM yourtable AS main
INNER JOIN yourtable AS alt ON (main.Key1 = alt.Key2) AND (main.Key2 = alt.Key1)

答案 2 :(得分:-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])

获得DEDUPLICATE:

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                   

:)感到误解了你