I have a table like below, placed at SQL FIDDLE
id empid proj file rating
~~ ~~~~~ ~~~~ ~~~~ ~~~~~~
1 1863 G1 file1 1
2 P4645 G1 file2 1
3 P6682 G1 file3 1
4 P6682 G1 file4 1
5 P4645 G1 file5 1
6 P4645 G1 file6 1
7 1863 G1 file7 1
8 1863 G1 file8 1
9 1863 G1 file19 1
10 P6682 G2 file21 1
11 1863 G2 file55 1
12 P6682 G2 file60 1
13 P4645 G2 file85 1
14 P6682 G2 file91 1
15 P4645 G2 file93 1
16 P4645 G3 file95 1
17 P4645 G3 file100 1
18 P4645 G3 file125 1
19 1863 G3 file131 1
20 1863 G3 file150 1
21 P6682 G3 file193 1
21 P6682 G3 file193 1
I need an SQL to retrieve top n (say top 2) ratings count from the above table for each category (proj)
Desired OP as below
proj empid ratings
~~~~ ~~~~~ ~~~~~~~
G1 1863 4
G1 P4645 3
G2 P6682 3
G2 P4645 2
G3 P4645 3
G3 1863 2
G3 P6682 2
Tried to follow the meathod in this thread but could not achive from the table desing i have.