Here's an example based on Scott's schema.
Employees, sorted by department numbers and their names:
Code:
SQL> select deptno, ename from emp
2 order by deptno, ename;
DEPTNO ENAME
---------- ----------
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
14 rows selected.
SQL>
I want to select 2 employees per each department; here's one option:
Code:
SQL> select deptno, ename
2 from
3 (select deptno,
4 ename,
5 row_number() over (partition by deptno order by deptno, ename) rn
6 from emp
7 )
8 where rn <= 2;
DEPTNO ENAME
---------- ----------
10 CLARK
10 KING
20 ADAMS
20 FORD
30 ALLEN
30 BLAKE
6 rows selected.
SQL>
Now, apply that to your data.