select a.id,a.name,
b.time,b.marryContion,
c.workbeginendtime,c.workAddr,c.job
from jbzl a
left join hyzk b on a.id=b.id
and not exists(
select * from hyzk where id=b.id and time>b.time)
left join gzll c on a.id=c.id
and not exists(
select * from gzll where id=c.id and workbeginendtime>c.workbeginendtime)
b.time,b.marryContion,
c.workbeginendtime,c.workAddr,c.job
from jbzl a
left join hyzk b on a.id=b.id
and not exists(
select * from hyzk where id=b.id and time>b.time)
left join gzll c on a.id=c.id
and not exists(
select * from gzll where id=c.id and workbeginendtime>c.workbeginendtime)
from(
select *
from jbzl a
inner join hyzk b
on a.id=b.id
inner join gzll c
on b.id=c.id
order by a.name,convert(datetime,substring(ltrim(c.beginendtime),1,charindex(ltrim(c.beginendtime),'-')-1,120)
)tselect *
from #t a
where not exists(select 1 from #t where name=a.name
and convert(datetime,substring(ltrim(beginendtime),1,charindex(ltrim(beginendtime),'-')-1,120)>a.convert(datetime,substring(ltrim(beginendtime),1,charindex(ltrim(beginendtime),'-')-1,120))drop table #t