日期:2014-05-16 浏览次数:21077 次
SELECT T1.*, T2.COUNT
FROM T_PRODUCT T1,
(SELECT T1.PID, T1.COUNT
FROM T_RECORD T1,
(SELECT MAX(T.ID) ID, T.PID FROM T_RECORD T GROUP BY T.PID) T2
WHERE T1.ID = T2.ID
AND T1.PID = T2.PID) T2
WHERE T1.ID = T2.PID(+)
with t_product as
(
select 1 id, 'p_name_1' name
from dual
union
select 2 id, 'p_name_2' name
from dual
union
select 3 id, 'p_name_3' name from dual),
t_record as
(select 1 id, 1 pid, 5 count
from dual
union
select 2 id, 1 pid, 6 count
from dual
union
select 3 id, 2 pid, 7 count from dual)
select a.id, a.name, b.count
from t_product a,
(select pid, max(count) KEEP(DENSE_RANK FIRST ORDER BY id desc) count
from t_record
group by pid) b
where a.id = b.pid(+)