删除表table1中重复记录,只保留一条记录。
一、指某两条或多条记录的所有字段值均相同,包括主键ID也相同。二、指某两条或多条记录的其中两个字段A、B相同,主键ID为INT自增型。就这两题,分别用SQL语句做,谢谢大家了,最好有详细的解释,本人是菜鸟~~

解决方案 »

  1.   

    http://blog.csdn.net/dba_huangzj/article/details/7699166看看能不能帮到你
      

  2.   

    LZ的"一、指某两条或多条记录的所有字段值均相同,包括主键ID也相同"这句话我没看懂,
    既然ID是主键了,怎么可能还会重复呢?
      

  3.   


    declare @table1 table(ID int identity(1,1) primary key,A int,B int,C int)
    insert into @table1
    select 1,2,3 union all
    select 2,2,3 union all
    select 1,3,3 union all
    select 1,2,3 union all
    select 1,2,3 union all
    select 3,2,1 union all
    select 1,2,3 union all
    select 1,3,3 union all
    select 4,2,3 union all
    select 1,4,3delete @table1
    from @table1 a
    inner join (select row_number() over(partition by A,B order by ID) rn,* from @table1) b
    on a.ID=b.ID and a.A=b.A and a.B=b.B and b.rn>1select * from @table1/*
    ID          A           B           C
    ----------- ----------- ----------- -----------
    1           1           2           3
    2           2           2           3
    3           1           3           3
    6           3           2           1
    9           4           2           3
    10          1           4           3
    */
      

  4.   

    问题没有任何问题,都是考卷上面原样COPY下来的。
      

  5.   

    如果是单个主键的话不可能重复的,你自己用我的例子试下就知道了,而如果是联合主键的话,比如字段p1和p2是联合主键,那么其中p1或p2是可以重复的。