Solved

Oracle Rownum

Posted on 2004-04-18
4
5,147 Views
Last Modified: 2009-09-30
i have this query

select deptno,sal,rownum
from
(
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????
0
Comment
Question by:sindhuanand
4 Comments
 
LVL 6

Accepted Solution

by:
musdu earned 50 total points
Comment Utility
Hi,

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

regards
0
 
LVL 13

Expert Comment

by:riazpk
Comment Utility
This is Easy:


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


Analytics Rock...Analytics Roll


BUT

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> /

    DEPTNO        SAL ROW_NUMBER()OVER(PARTITIONBYDE
========== ========== ==============================
        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> /

     EMPNO ENAME          DEPTNO        SAL ROW_NUMBER()OVER(PARTITIONBYDE
========== ========== ========== ========== ==============================
      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) ?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by 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…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now