samedi 23 juillet 2016

Oracle query to find the top 2 item per group

I want to right a sql query to find the top 2 item per group in oracle.
The table contains:

P_id Price GroupX
1     10    a 
2     5     a
3     5     a
4     4     b
5     8     b
6     6     b

Output should be:

P_id Price  GroupX
1     10      a
2     5       a
5     8       b
6     6       b

What I have tried is:

Select * from table
group by GroupX
order by price desc
limit 2

Aucun commentaire:

Enregistrer un commentaire