如何删除重复记录?

时间:2015-04-17 08:40:12

标签: sql

我有重复记录的SQL表但是一个字段不同 如何删除此文件为空的重复记录?
如果两对为空则删除一个

my table is :
id Number No
1 A25 10
2 A24 20
3 A25  
4 C12
5 C12 

new table : 
id Number No 
1 A25 10 
2 A24 20 
4 C12
5 C12   

3 个答案:

答案 0 :(得分:2)

试试这个。

select *
into    #temp1
from    My_table 
group by Number
having Count(*)>1

delete  My_table 
from    My_table 
join    #temp1
    on   My_table.Number = #temp1.Number
where    My_table.Number is null 

答案 1 :(得分:2)

delete from mytable where id in(
    select t1.id
    from mytable t join mytable t1 on t.number=t1.number and t1.id>t.id
    where (t1.no is null and t.no is not null) or (t1.no is null and t.no is null)
)

答案 2 :(得分:1)

<强> 1。溶液

delete from emp
    where rowid not in
    (select max(rowid) from emp group by empno);

<强> 2。 SLOUTION

delete from emp where rowid in
               (
                 select rid from
                  (
                    select rowid rid,
                      row_number() over(partition by empno order by empno) rn
                      from emp
                  )
                where rn > 1
               );

<强> 3.solution

delete from emp e1
         where rowid not in
          (select max(rowid) from emp e2
           where e1.empno = e2.empno );

<强> 4。溶液

 delete from emp where rowid in
            (
             select rid from
                (
                  select rowid rid,
                  dense_rank() over(partition by empno order by rowid
                ) rn
             from emp
            )
 where rn > 1
);