日期:2014-05-17 浏览次数:20950 次
WITH t1 AS( SELECT 1 id,'00XX' topic FROM dual UNION ALL SELECT 2,'XX00' FROM dual UNION ALL SELECT 3,'####'FROM dual UNION ALL SELECT 4,'####'FROM dual UNION ALL SELECT 5,'####'FROM dual UNION ALL SELECT 6,'####'FROM dual UNION ALL SELECT 7,'####'FROM dual ), t2 AS( SELECT 1 id,1 topic_id,3333 info FROM dual UNION ALL SELECT 2,2,11111 FROM dual UNION ALL SELECT 4,1,252 FROM dual UNION ALL SELECT 5,2,991 FROM dual UNION ALL SELECT 6,4,137 FROM dual UNION ALL SELECT 8,1,711 FROM dual UNION ALL SELECT 10,1,267 FROM dual UNION ALL SELECT 11,5,75 FROM dual UNION ALL SELECT 12,6,4 FROM dual ) SELECT t1.id topic_id,Nvl(temp.id,0)revert_id,temp.info FROM t1 left join ( SELECT id,topic_id,info FROM ( SELECT id,topic_id,info,Row_Number() over (PARTITION BY topic_id ORDER BY id desc)rn FROM t2 )WHERE rn=1 )temp ON t1.id=temp.topic_id ORDER BY t1.id;
------解决方案--------------------
select c.topic,c.id,d.info
from revort_table d,(select a.topic,max(b.id) id
from topic_table a,revort_table b
where a.topic = b.topic
group by a.topic) c
where c.id = d.id
------解决方案--------------------
WITH t1 AS(
SELECT 1 id,'00XX' topic FROM dual
UNION ALL
SELECT 2,'XX00' FROM dual
UNION ALL
SELECT 3,'####'FROM dual
UNION ALL
SELECT 4,'####'FROM dual
UNION ALL
SELECT 5,'####'FROM dual
UNION ALL
SELECT 6,'####'FROM dual
UNION ALL
SELECT 7,'####'FROM dual
), t2 AS(
SELECT 1 id,1 topic_id,3333 info FROM dual
UNION ALL
SELECT 2,2,11111 FROM dual
UNION ALL
SELECT 4,1,252 FROM dual
UNION ALL
SELECT 5,2,991 FROM dual
UNION ALL
SELECT 6,4,137 FROM dual
UNION ALL
SELECT 8,1,711 FROM dual
UNION ALL
SELECT 10,1,267 FROM dual
UNION ALL
SELECT 11,5,75 FROM dual
UNION ALL
SELECT 12,6,4 FROM dual
)
select * from t1,t2 where t1.id = topic_id and (t1.id,t2.id) in (
select t1.id,max(t2.id) from t1,t2 where t1.id = topic_id
group by t1.id)