select  a.c1,a.c2, b.c1,(select c4  from  bbb  where c1=b.c1 and c2=b.c2)  as c4from  aaa  a , (select max(c1) as c1,c2 from bbb where  c4 is not null  group by  c2)  b 
where a.c1=b.c2