有如下一个表:姓名1  姓名2  年份
张三   李四    2001
王五   赵六    2003
张三   王五    2004
张三   李四    2007
张三   李四    2008现在要求是  将姓名1和姓名2是一样的就保留一条,其余相同的删除,就如上面的第一 四 五条数据的姓名1都是张三,姓名2都是李四的就保留第一条,后面第四条第五条删除,年份相不相同没关系。也可以随意删除三个相同数据的任意一条,只要保留一条就行!谢谢大家

解决方案 »

  1.   

    delete a
    from tb a
    where exists(select 1 from a.姓名1=姓名1 and a.姓名2=姓名2 and 年份<a.年份)
      

  2.   

    declare @t table(姓名1 varchar(10),姓名2 varchar(10),年份 int)
    insert @t select '张三' ,  '李四'  ,  2001 
    insert @t select '王五' ,  '赵六'  ,  2003 
    insert @t select '张三' ,  '王五'  ,  2004 
    insert @t select '张三' ,  '李四'  ,  2007 
    insert @t select '张三' ,  '李四'  ,  2008 delete a 
    from @t a 
    where exists(select 1 from @t where a.姓名1=姓名1 and a.姓名2=姓名2 and 年份 <a.年份)select * from @t/*
    姓名1        姓名2        年份
    ---------- ---------- -----------
    张三         李四         2001
    王五         赵六         2003
    张三         王五         2004(3 行受影响)
    */
      

  3.   

    declare @t table(姓名1 varchar(10),姓名2 varchar(10),年份 int)
    insert @t select '张三' ,  '李四'  ,  2001 
    insert @t select '王五' ,  '赵六'  ,  2003 
    insert @t select '张三' ,  '王五'  ,  2004 
    insert @t select '张三' ,  '李四'  ,  2007 
    insert @t select '张三' ,  '李四'  ,  2008 delete a 
    from @t a 
    where exists(select 1 from @t where a.姓名1=姓名1 and a.姓名2=姓名2 and 年份 >a.年份)select * from @t/*
    姓名1        姓名2        年份
    ---------- ---------- -----------
    王五         赵六         2003
    张三         王五         2004
    张三         李四         2008(3 行受影响)*/
      

  4.   


    select 姓名1,姓名2,min(年份) from 表名 group by 姓名1,姓名2
      

  5.   

    declare @t table(姓名1 varchar(10),姓名2 varchar(10),年份 int)
    insert @t select '张三' ,  '李四'  ,  2001 
    insert @t select '王五' ,  '赵六'  ,  2003 
    insert @t select '张三' ,  '王五'  ,  2004 
    insert @t select '张三' ,  '李四'  ,  2007 
    insert @t select '张三' ,  '李四'  ,  2008 
    select * from @T a where 年份=(select top 1 年份  from @t where  姓名1=a.姓名1 and 姓名2=a.姓名2)select 
    姓名1,
    姓名2,
    年份=(select top 1 年份 from @t where  姓名1=a.姓名1 and 姓名2=a.姓名2 order by newID())--随机
    from @T  a
    group by 姓名1,姓名2姓名1        姓名2        年份          
    ---------- ---------- ----------- 
    张三         李四         2001
    王五         赵六         2003
    张三         王五         2004(所影响的行数为 3 行)姓名1        姓名2        年份          
    ---------- ---------- ----------- 
    王五         赵六         2003
    张三         李四         2008
    张三         王五         2004(所影响的行数为 3 行)
      

  6.   

    --删除
    delete a from @T a where not in (select top 1 年份  from @t where  姓名1=a.姓名1 and 姓名2=a.姓名2)
      

  7.   

    --deletedelete a from @T a where 年份 not in (select top 1 年份  from @t where  姓名1=a.姓名1 and 姓名2=a.姓名2 )
      

  8.   


    --删除
    delete 表名 from 表名 a where exists(select 1 from 表名 where 姓名1=a.姓名1 and 姓名2=a.姓名2 and 年份>a.年份)
      

  9.   

    declare @t table(姓名1 varchar(10),姓名2 varchar(10),年份 int)
    insert @t select '张三' ,  '李四'  ,  2001 
    insert @t select '王五' ,  '赵六'  ,  2003 
    insert @t select '张三' ,  '王五'  ,  2004 
    insert @t select '张三' ,  '李四'  ,  2007 
    insert @t select '张三' ,  '李四'  ,  2008 
    select * from @tdelete a 
    from @t a
    where  exists(select 1 from @t where 姓名1= a.姓名1 and 姓名2 = a.姓名2 and 年份< a.年份)select * from @t
    姓名1        姓名2        年份          
    ---------- ---------- ----------- 
    张三         李四         2001
    王五         赵六         2003
    张三         王五         2004
    张三         李四         2007
    张三         李四         2008(所影响的行数为 5 行)
    (所影响的行数为 2 行)姓名1        姓名2        年份          
    ---------- ---------- ----------- 
    张三         李四         2001
    王五         赵六         2003
    张三         王五         2004(所影响的行数为 3 行)