分析を使用できます:
SQL> WITH DATA AS (
2 SELECT 1 pid, 123 quote_id, 'A' pdf1 FROM DUAL
3 UNION ALL SELECT 2 pid, 123 quote_id, 'B' pdf1 FROM DUAL
4 UNION ALL SELECT 3 pid, 123 quote_id, 'C' pdf1 FROM DUAL
5 UNION ALL SELECT 4 pid, 152 quote_id, 'D' pdf1 FROM DUAL
6 UNION ALL SELECT 5 pid, 888 quote_id, 'E' pdf1 FROM DUAL
7 )
8 SELECT DISTINCT first_value(pid) over (ORDER BY pid DESC) pid,
9 quote_id,
10 first_value(pdf1) over (ORDER BY pid DESC) pdf1
11 FROM DATA
12 WHERE quote_id = 123;
PID QUOTE_ID PDF1
---------- ---------- ----
3 123 C
集計を使用することもできます:
SQL> WITH DATA AS (
2 SELECT 1 pid, 123 quote_id, 'A' pdf1 FROM DUAL
3 UNION ALL SELECT 2 pid, 123 quote_id, 'B' pdf1 FROM DUAL
4 UNION ALL SELECT 3 pid, 123 quote_id, 'C' pdf1 FROM DUAL
5 UNION ALL SELECT 4 pid, 152 quote_id, 'D' pdf1 FROM DUAL
6 UNION ALL SELECT 5 pid, 888 quote_id, 'E' pdf1 FROM DUAL
7 )
8 SELECT MAX(pid),
9 quote_id,
10 MAX(pdf1) KEEP (DENSE_RANK FIRST ORDER BY pid DESC) pdf1
11 FROM DATA
12 WHERE quote_id = 123
13 GROUP BY quote_id;
MAX(PID) QUOTE_ID PDF1
---------- ---------- ----
3 123 C