Oracle Rownum

Posted on 2004-04-18
Last Modified: 2009-09-30
i have this query

select deptno,sal,rownum
select deptno,sum(sal) sal
from emp
group by deptno)

this will give me the rownum....but i want to reset the rownum when the dept changes.How can you do this or is there any other way????
Question by:sindhuanand
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

musdu earned 50 total points
ID: 10857124

select deptno,sal, rank() over (partition by deptno order by rownum) as seq
select deptno,sum(sal) sal
from emp
group by deptno)

LVL 13

Expert Comment

ID: 10857134
This is Easy:

sect deptno,sal,row_number() over (partition by deptno order by '1')
(select deptno,sum(sal) sal
from emp
group by deptno)

Analytics Rock...Analytics Roll


my question is:

since you are grouping by deptno, so if you want to reset the rownum for each deptno, then each deptno will have rownum 1 eactly...this doesn't makes sense to me. For Example:
  1  select deptno,sal,row_number() over (partition by deptno order by '1')
  2  from
  3  (select deptno,sum(sal) sal
  4  from test.emp
  5* group by deptno)
SQL> /

========== ========== ==============================
        10       8750                              1
        20      10075                              1
        30       9480                              1

However, this may makes sense:

  1  select empno,ename,deptno,sal,row_number() over (partition by deptno order by '1')
  2* from test.emp
SQL> /

========== ========== ========== ========== ==============================
      7782 CLARK              10       2450                              1
      7839 KING               10       5000                              2
      7934 MILLER             10       1300                              3
      7566 JONES              20       2975                              1
      7902 FORD               20       3000                              2
      7876 ADAMS              20       1100                              3
      7788 SCOTT              20       3000                              4
      7499 ALLEN              30       1680                              1
      7900 JAMES              30        950                              2
      7844 TURNER             30       1500                              3
      7654 MARTIN             30       1250                              4
      7698 BLAKE              30       2850                              5
      7521 WARD               30       1250                              6

Would you please tell us what is reason behind getting such output (as you described) ?

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post first appeared at Oracleinaction  ( Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question