表name:身份证号为sfzh;
sfzh    zzdz    pcs   jwh    djrq
0123    北京    001   00101  061120
0123    江西    001   00102  061110
0456    浙江    001   00103  061020
0456    江西    001   00108  061120
0789    江西    001   00106  061121
我现在想实现 sfzh相同的,只要 djrq 大的数据
结果为:
sfzh    zzdz    pcs   jwh    djrq
0123    北京    001   00101  061120
0456    江西    001   00108  061120
0789    江西    001   00106  061121
请问,sql语句应该怎么写啊??

解决方案 »

  1.   

    delete a
    from 表name a
    where exists (
    select 1 from 表name
    where sfzh=a.sfzh
    and djrq >a.djrq 
    )
      

  2.   

    select * from name a
    where not exists (select 1 from name where sfzh =a.sfzh and djrq>a.djrq)
      

  3.   

    or:delete a
    from 表name a
    where djrq< (
    select max(djrq) from 表name
    where sfzh=a.sfzh
    )
      

  4.   

    --如果只是查询:
    select a.* from name a where not exists(select 1 from name where sfzh=a.sfzh and a.djrq<djrq)
      

  5.   

    delete a
    from 表name a
    where djrq< (
    select max(djrq) from 表name
    where sfzh=a.sfzh
    )
      

  6.   

    建议写个存储过程
    仅仅一条SQL语句是无法实现的.
    create procedure addProc
    as
    declare @sfzh varchar
    begin
    if not exists (select * from [newtable] where @sfzh=sfzh in (select sfzh from [name]))
      insert into [newtable] (sfzh,...) values (@sfzh,...)
    end
      

  7.   

    sfzh    zzdz    pcs   jwh    djrq
    0123    北京    001   00101  061120
    0123    江西    001   00102  061110
    0456    浙江    001   00103  061020
    0456    江西    001   00108  061120
    0789    江西    001   00106  061121select name.* from name
    (select sfzh,max(djrq) djrq from name
    group by sfzh) a
    where name.sfzh = a.sfzh and name.djrq = a.djrq
      

  8.   

    select 1 是什麽意思啊?
      

  9.   

    select * into ff from name a
    where not exists (select 1 from name where sfzh =a.sfzh and djrq>a.djrq)这个不成? 我实验了. 更你的要求一样??
      

  10.   

    好像还是没有剔除干净啊~~
    我想问一下:
    not exists (select 1 from name where sfzh =a.sfzh and djrq>a.djrq)
    这一句话,怎么理解啊~~
    select 1 什么意思 djrq>a.djrq又怎么理解
      

  11.   

    djrq是不是一定要唯一的啊??如果有相同的,那是不是不行了呢??
      

  12.   

    create table [name](sfzh char(4),    zzdz nvarchar(10),    pcs char(3),  jwh char(5),    djrq char(6))
    insert [name] select '0123',    '北京',    '001',   '00101',  '061120'
    union all select '0123',    '江西',    '001',   '00102',  '061110'
    union all select '0456',    '浙江',    '001',   '00103',  '061020'
    union all select '0456',    '江西',    '001',   '00108',  '061120'
    union all select '0789',    '江西',    '001',   '00106',  '061121'delete [name] 
    where exists(select * from [name] as B where B.sfzh=[name].sfzh and B.djrq>[name].djrq)select * from [name]--result
    sfzh zzdz       pcs  jwh   djrq   
    ---- ---------- ---- ----- ------ 
    0123 北京         001  00101 061120
    0456 江西         001  00108 061120
    0789 江西         001  00106 061121(3 row(s) affected)
      

  13.   

    方法是对的!!但我不理解什么意思啊~~能不能解释一下啊~~
    我想问一下:
    not exists (select 1 from name where sfzh =a.sfzh and djrq>a.djrq)
    这一句话,怎么理解啊~~
    select 1 什么意思 djrq>a.djrq又怎么理解
      

  14.   

    delete a
    from 表name a
    where not exists (
    select 1 from 表name
    where sfzh=a.sfzh
    and djrq <a.djrq 
    )not exits是表示在子查询中不存在的记录
      

  15.   

    如果sfzh相同时,并且djrq不相同时可以用以下的语句:
    delete sfzhtable 
    where exists(select sfzh from sfzhtable as A where A.sfzh=sfzhtable.sfzh and A.djrq>sfzhtable.djrq)