select b.b_id, b.s_id, b.cr_date, a.name, a.sname from b
left join a on b.s_id=a.s_id
where a.c_s=1
order by b.cr_date desc
------其他解决方案--------------------
SELECT *
FROM a t1
WHERE c_s = 1
AND EXISTS
(SELECT 1 FROM b WHERE b.s_id = t1.s_id AND TO_CHAR(cr_date,'yyyy') = '2012'
);
------其他解决方案-------------------- 我写了一个:
with a as
(select '1' s_id, 'a' name, 'aa' sname, '1' c_s
from dual
union all
select '2' s_id, 'a' name, 'aa' sname, '1' c_s from dual),
b as
(select '1' b_id, '1' s_id, date '2012-10-10' cr_date
from dual
union all
select '2' b_id, '1' s_id, date '2011-10-10' cr_date
from dual
union all
select '3' b_id, '2' s_id, date '2012-09-09' cr_date from dual)
select t.s_id, t.name, t.sname, t.c_s, t.max_date
from (select distinct a.*, max(b.cr_date) over(partition by b.s_id) max_date
from a, b
where a.s_id = b.s_id(+)
and a.c_s = '1'
order by max_date) t
order by decode(extract(year from t.max_date), extract(year from sysdate), t.max_date, null) ------其他解决方案--------------------
select * from
(select distinct a.*,row_number() over(partition by b.b_id order by b.cr_date)rn
from a,b
where a.s_id = b.s_id(+)
and a.c_s = '1' )t
where t.rn =1 ------其他解决方案--------------------