select a.ANAME,b.BNAME from A as a
inner join
(select AID,BNAME from B as t where
not exists(select 1 from B where AId = t.AId and BTime > t.BTime)) as b
on a.AId = b.AId
inner join
(select AID,BNAME from B as t where
not exists(select 1 from B where AId = t.AId and BTime > t.BTime)) as b
on a.AId = b.AId
(select a1.bname,aid from (select BNAME,max(BTime) btime from B group by bname) a1 join b a2 on a1.bname=a2.bname and a1.btime=a2.btime) as b
on a.AId = b.AId
create table B(BId int,BName varchar(100),BTime datetime,AId int)
--SQL
select A.AName,TT.BName
from A inner join
(select B.AId,B.BName from (select AId,max(BTime) as BTime from B group by AId) T join B on T.AId=B.AId and T.BTime=B.BTime) as TT
on A.AId = TT.AIddrop table A,B
from a left join b
on a.aid=b.aid
and b.BTime=(select max(BTime) from b where aid=b.aid)
a.aname,b.bname
from
a, b, (select aid,max(btime) as btime from b group by aid) b1
where
a.aid=b.aid and b.aid=b1.aid and b.btime=b1.btime
order by b.bid