because UNLESS AND UNTIL YOU HAVE AN ORDER BY ON THE QUERY YOU HAVE NO REASON TO EXPECT THE DATA TO BE SORTED AT ALL.
...
select e.deptno,e.ename, e.job,
row_number() over(partition by e.deptno order by case when job='MANAGER'
then 0 else 1 end, job) rn
from emp e ;
....
that query does NOT show the manager first.
that query assigned a number "RN" to each row sequetially, after sorting the data by the CASE statement. That the rows accidentally were sorted was just that - an ACCIDENT and not assured to happen in real life.
Until you have ORDER BY, you cannot expect data to be sorted!!!!! order by on the STATMENT, not in an analytic function - IN THE STATEMENT.
You meant to use:
ops$tkyte%ORA11GR1> select e.deptno,e.ename, e.job
2 from scott.emp e
3 order by deptno, case when job = 'MANAGER' then 0 else 1 end;
DEPTNO ENAME JOB
---------- ---------- ---------
10 CLARK MANAGER
10 KING PRESIDENT
10 MILLER CLERK
20 JONES MANAGER
20 SCOTT ANALYST
20 FORD ANALYST
20 ADAMS CLERK
20 SMITH CLERK
30 BLAKE MANAGER
30 ALLEN SALESMAN
30 TURNER SALESMAN
30 JAMES CLERK
30 WARD SALESMAN
30 MARTIN SALESMAN
14 rows selected.
Your "partition by" and "order by" in the analytic do not mean, imply, infer that the data would be sorted in any way shape or form
If you learn one thing today make it be this:
UNLESS AND UNTIL YOUR SQL ENDS WITH "ORDER <sibilings> BY", you have no reason to expect the data to be retrieved in any order.
NOTHING short of ORDER BY orders data, group by does not assure your data is sorted, neither do analytics, INDEX hints - NOTHING but ORDER BY on the end of the query does that