表A
档案号  姓名    身份证    部门
aa001   a       1      aa
bb001   a       1      bb
cc001   a       1      cc表B
档案号    部门删除表A中身份证一样的记录,只保留一条,一个档案号.表B插入保留下来的档案号,部门结果
表A
档案号  姓名    身份证    部门
aa001   a       1      null
(档案号可以其他任意一个)表B
档案号    部门
aa001    aa
aa001    bb
aa001    cc

解决方案 »

  1.   

    inesrt into B select 档案号,部门 from Adelete from A t where 档案号 not in (select min(档案号) from A where 身份证 = t.身份证)
      

  2.   

    select *
    into #tmp
    from ta a
    where not exists(select 1 from ta where 身份证 = a.身份证 and 档案号 > a.档案号)
    insert into tb
    select a.档案号,b.部门
    #tmp a right join ta b on 1= 1
    truncate table tainsert into ta select 档案号,姓名,身份证,null from #tmpdrop table #tmp
      

  3.   


    create table A(dh varchar(10),xm varchar(10),sfz varchar(20),bm varchar(10))
    insert into A select 'aa001','a','1','aa' union all select 'bb001','a','1','bb'
          union all select 'cc001','a','1','cc'
    --插入B表数据
    select dh,bm into B from Aselect * from A
    --删除重复数据
    --方法1
    delete T from A T where dh not in(select min(dh) from A where sfz = T.sfz )
    --方法2(留下最小的)
    delete T from A T where   exists(select 1 from A  where sfz = T.sfz and dh <T.dh)--方法3(留下最大的)
    delete T from A T where  exists(select 1 from A where sfz = T.sfz and dh >T.dh)drop table A,B
      

  4.   

    INSERT b SELECT (SELECT min(档案号) from a where 身份证=a.身份证 group by 身份证),部门 FROM a a
    DELETE FROM a WHERE EXISTS(SELECT 1 FROM a aa WHERE 身份证=a.身份证 and 档案号>a.档案号)
      

  5.   

    结果 
    表A 
    档案号  姓名    身份证    部门 
    aa001   a       1      null 
    (档案号可以其他任意一个) 表B 
    档案号    部门 
    aa001    aa 
    aa001    bb 
    aa001    cc注意表B的档案号
      

  6.   

    insert into b (档案号,部门)
    select 档案号,部门 from a
    group by 档案号,部门
      

  7.   

    insert into b (档案号,部门)
    select a.档案号,b.部门
    from 
    (select distinct 档案号 from a) a,(select distinct 部门 from a) b
      

  8.   

    结果 
    表A 
    档案号  姓名    身份证    部门 
    aa001   a       1      null 
    (档案号可以其他任意一个) 表B 
    档案号    部门 
    aa001    aa 
    aa001    bb 
    aa001    cc表B的档案号必须跟表A保留下来的档案号一样
      

  9.   


    7楼的不是实现了你的功能了吗,用cross join
      

  10.   

    上面的有点小失误,把后面的别名a改成aa
    INSERT b SELECT (SELECT min(档案号) from a where 身份证=aa.身份证),部门 FROM a aa
    DELETE FROM a WHERE EXISTS(SELECT 1 FROM a aa WHERE 身份证=a.身份证 and 档案号>a.档案号)
      

  11.   

    /******************************************/
    /*回复:代码20080512006 总:00000000006    */
    /*主题:删除表A中身份证一样的记录           */
    /*作者:二等草                             */
    /******************************************/
    set nocount on
    /************例子数据 begin****************/
    declare  @ta table(ID varchar(10),name varchar(10),idcode varchar(10),dept varchar(10))
    insert @ta select 'aa001',   'a',      1,      'aa' 
    insert @ta select 'bb001',   'a',      1,      'bb' 
    insert @ta select 'cc001',   'a',      1,      'cc' 
    declare @tb table(id varchar(10),dept varchar(10))
    /************例子数据 end******************//************代码     begin***************/
    insert @tb select id=(select min(id) from @ta where idcode = a.idcode),dept from @ta a
    delete a from @ta a,(select idcode,min(id) id from @ta group by idcode) b where a.idcode = b.idcode and a.id <> b.id
    select * from @ta
    select * from @tb/************代码     end*****************//************结果     begin***************
    ID         name       idcode     dept       
    ---------- ---------- ---------- ---------- 
    aa001      a          1          aaid         dept       
    ---------- ---------- 
    aa001      aa
    aa001      bb
    aa001      cc ************结果     end*****************//************清除*************************/