It was hard to come up with an understandable title for this question. I\'ll try to explain with an example.
First I had a simple table INFO
in Oracle
Analytic functions are your friend:
SELECT MAX( year ) KEEP ( DENSE_RANK LAST ORDER BY year ASC, quarter ASC, message ASC ) AS year,
MAX( quarter ) KEEP ( DENSE_RANK LAST ORDER BY year ASC, quarter ASC, message ASC ) AS quarter,
MAX( message ) KEEP ( DENSE_RANK LAST ORDER BY year ASC, quarter ASC, message ASC ) AS message,
type
FROM info
GROUP BY type;
SQLFIDDLE
Give this a shot:
SELECT i.year, i.quarter, i.type, i.message
FROM INFO i
JOIN INFO iy
ON i.type = iy.type
JOIN INFO iq
ON iq.type = iy.type
WHERE i.type IN (1,2)
GROUP BY i.year, i.quarter, i.type, i.message
HAVING i.year = MAX(iy.year) AND i.quarter = MAX(iq.quarter)