删除没有任何主要ID的重复记录

时间:2013-09-11 07:22:32

标签: sql-server

我有一个名为ComplimentTransactAssign_tbl的表。在此表中有许多重复的条目。表结构如下:

TransactID           Cmplid     
-
32                      16          
105                     17          
105                     17        
290                     12         
32                      16
290                     12

我发现我的重复记录如下:

select TransactID  from ComplimentTransactAssign_tbl
  group by TransactID having count(*) >1 order by TransactID

我想删除重复记录。删除后我想得到这样的输出:

TransactID           Cmplid     
-
32                      16          
105                     17                 
290                     12         

1 个答案:

答案 0 :(得分:2)

;WITH MyCTE AS
(
    SELECT TransactID ,
           Cmplid,
           ROW_NUMBER() OVER(PARTITION BY TransactID ORDER BY TransactID) AS row_num
    FROM ComplimentTransactAssign_tbl cta
)   
DELETE FROM MyCTE 
WHERE row_num <> 1

SQL Fiddle Example