从我的表中删除重复的行

时间:2014-06-23 13:04:11

标签: sql ms-access

我需要删除表格中的所有重复行 - 但只留下一行

MyTbl
====
Code     |  ID  |  Place  |  Qty  |  User
========================================
1        |  22 |   44     |  34   |  333
2        |  22 |   44     |  34   |  333
3        |  22 |   55     |  34   |  333
4        |  22 |   44     |  34   |  666
5        |  33 |   77     |  12   |  999
6        | 44  |   11     |  87   |  333
7        | 33  |   77     |  12   |  999

我需要看到这个:

Code    | ID  |  Place  |  Qty  |  User
=======================================
1       | 22 |   44     |  34   |  333
3       | 22 |   55     |  34   |  333
4       | 22 |   44     |  34   |  666
5       | 33 |   77     |  12   |  999
6       | 44 |   11     |  87   |  333

2 个答案:

答案 0 :(得分:2)

在大多数数据库中,最快的方法是:

select distinct t.*
into saved
from mytbl;

delete from mytbl;

insert into mytbl
    select *
    from saved;

上述语法应该在Access中有效。其他数据库将使用truncate table而不是delete

答案 1 :(得分:-1)

试试这个,

 WITH CTEMyTbl (A,duplicateRecCount)
    AS
    (  
    SELECT id,ROW_NUMBER() OVER(PARTITION by id,place,qty,us  ORDER BY id)
    AS duplicateRecCount   
    FROM MyTbl
    )
    DELETE FROM CTEMyTbl
    WHERE duplicateRecCount > 1