What about select distinct id, smallest, largest, rank () over (order by smallest, largest) as rank from ( select distinct id, smallest, largest from (select id , min(value) over (partition by id) smallest , max(value) over (partition by id) largest from zzz ) ) order by rank ; Analytic functions all the way :-) jan