Friday, September 11, 2009

HOW TO SHOW FIRST ROW OF THE GROUP

Sql> select employee_id,first_name, department_id
2 from
3 (
4 select e.employee_id,e.first_name,d.department_id,
5 row_number() over (partition by d.department_id order by d.department_id asc) rn
6 from employees e,departments d
7 where e.department_id=d.department_id
8 order by d.department_id asc
9 )
10 where rn = 1;

EMPLOYEE_ID FIRST_NAME DEPARTMENT_ID
----------- -------------------- -------------
200 Jennifer 10
201 Michael 20
114 Den 30
203 Susan 40
198 Donald 50
103 Alexander 60
204 Hermann 70
145 John 80
100 Steven 90
108 Nancy 100
205 Shelley 110

11 rows selected.

No comments: