表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语句应该怎么写啊??
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语句应该怎么写啊??
from 表name a
where exists (
select 1 from 表name
where sfzh=a.sfzh
and djrq >a.djrq
)
where not exists (select 1 from name where sfzh =a.sfzh and djrq>a.djrq)
from 表name a
where djrq< (
select max(djrq) from 表name
where sfzh=a.sfzh
)
select a.* from name a where not exists(select 1 from name where sfzh=a.sfzh and a.djrq<djrq)
from 表name a
where djrq< (
select max(djrq) from 表name
where sfzh=a.sfzh
)
仅仅一条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
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
where not exists (select 1 from name where sfzh =a.sfzh and djrq>a.djrq)这个不成? 我实验了. 更你的要求一样??
我想问一下:
not exists (select 1 from name where sfzh =a.sfzh and djrq>a.djrq)
这一句话,怎么理解啊~~
select 1 什么意思 djrq>a.djrq又怎么理解
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)
我想问一下:
not exists (select 1 from name where sfzh =a.sfzh and djrq>a.djrq)
这一句话,怎么理解啊~~
select 1 什么意思 djrq>a.djrq又怎么理解
from 表name a
where not exists (
select 1 from 表name
where sfzh=a.sfzh
and djrq <a.djrq
)not exits是表示在子查询中不存在的记录
delete sfzhtable
where exists(select sfzh from sfzhtable as A where A.sfzh=sfzhtable.sfzh and A.djrq>sfzhtable.djrq)