I have a table (myTab), which has 3 columns(id, marketValue, stock).
I want to group top n rows ordered by marketValue. I can think of this nested query.
SELECT AVG(marketValue), stock FROM (SELECT marketValue, stock FROM myTab
order by(marketValue) desc LIMIT(n)) GROUP BY stock.
Can we optimise it further? This is not to get top n rows of each group.
SAMPLE TABLE
If I group top 3 rows, the expected result should be :