I have a query along the lines of
select b.* from
(select key, max(val) as val from (somequery) group by key) as a
inner join
(somequery) as b
on a.key = b.key and a.val = b.val
order by key
And I was wondering if there is an obvious way (that I am missing) to simplify it (given that somequery might be rather long).
Any thoughts would be appreciated.