cinvcode  iprice  ddate
010001    10      2008-01-01   
020002    20      2009-01-05
010001    10      1008-01-01
010003    30      2009-03-31
010003    30      2009-03-31
。。
数据库中有很多类似于010001和010003这样的重复数据,我实际上只需要一行,怎样删除其它行?

解决方案 »

  1.   


    select distinct * from ta into #t
    delete from ta where  1=1
    insert ta select #t
      

  2.   

    delete tb
    where exists(select 1 from tb t where cinvcode=t.cinvcode and ddate>t.ddate)
      

  3.   

    alter table tb add int indentity(1,1)delete from tb t where exists(select 1 from tb where cinvcode=t.cinvcode and iprice=t.iprice   and ddate=t.ddate and id>t.id)
      

  4.   

    DELETE A FROM A T WHERE EXISTS(SELECT 1 FROM A WHERE cinvcode=T.cinvcode AND DDATE>T.DDATE)
      

  5.   

    select distinct * into # from tb 
    truncate table  tb 
    insert tb
    select *
    from # 
    delete tb 
    where exists(select 1 from tb t where cinvcode=t.cinvcode and ddate>t.ddate)
      

  6.   

    select distinct * into # from tb 
    truncate table  tb 
    insert tb
    select * from # 
    --如果是删除完全一样 这样
      

  7.   

    js_szy,josy的都不行。我再试一试其它人的。
      

  8.   


    select distinct * from ta into #t
    delete from ta where  1=1
    insert ta select * from #t漏东西了
      

  9.   

    select distinct * into # from tb 
    truncate table  tb 
    insert tb
    select * from # 
    就样就行了
      

  10.   


    完全一样的用个临时表处理select distinct * into # from tb
    truncate table tb
    insert tb select * from #
    drop table #
      

  11.   

    2005delete top(1) from TB where id=...
      

  12.   

    CREATE TABLE TB(cinvcode VARCHAR(10),  iprice INT, ddate DATETIME)
    INSERT TBSELECT '010001'   , 10  ,    '2008-01-01'  UNION ALL
    SELECT '020002'   , 20   ,   '2009-01-05' UNION ALL
    SELECT '010001'   , 10   ,   '2008-01-01' UNION ALL
    SELECT '010003'   , 30   ,   '2009-03-31' UNION ALL
    SELECT '010003'   , 30   ,   '2009-03-31'--DROP TABLE TB--SELECT * FROM TB
    SELECT IDD=IDENTITY(INT,1,1),* INTO # FROM TBDELETE # FROM # T WHERE EXISTS(SELECT 1 FROM # WHERE cinvcode=T.cinvcode AND IDD>T.IDD)SELECT * FROM #
    IDD         cinvcode   iprice      ddate                                                  
    ----------- ---------- ----------- ------------------------------------------------------ 
    2           020002     20          2009-01-05 00:00:00.000
    3           010001     10          2008-01-01 00:00:00.000
    5           010003     30          2009-03-31 00:00:00.000(所影响的行数为 3 行)
      

  13.   

    如果只有少量的重复行,知道条件,那么用游标吧。declare curt cursor for select * from tb where ....delete from tb where current of cursor
      

  14.   

    delete from tabel where cinvcode  not in(select max(cinvcode) from table group by cinvcode)
      

  15.   

    delete from tabel where id not in(select id from table group by cinvcode)
      

  16.   

    哎,又写错了delete from tabel where id not in(select max(id) from table group by cinvcode)