简体   繁体   中英

SQL Query to change rownum as text changes in Oracle

I'm stuck with a query wherein i need to change rownum or any running serial no. back to its original value as soon as it founds change of text of second column. I'm using Oracle 11g.

For Eg. My Data is

    ________ 
   | EMP_CD |
   |--------| 
   |  D123  |
   |--------| 
   |  D123  | 
   |--------| 
   |  D123  |
   |--------| 
   |  F743  |
   |--------|  
   |  F743  |
   |________|

So Now What I'm expecting is

 _______ ________ 
| SR NO | EMP_CD |
|-------|--------| 
|   1   |  D123  |
|-------|--------| 
|   2   |  D123  | 
|-------|--------| 
|   3   |  D123  |
|-------|--------| 
|   1   |  F743  |
|-------|--------|  
|   2   |  F743  |
|_______|________|
select row_number() over (partition by emp_cd order by null) as sr_no, emp_cd
from   your_table
order by emp_cd, sr_no;

ORDER BY is optional.

You can use the row_number() analytic function:

SELECT ROW_NUMBER() OVER (PARTITION BY emp_cd ORDER BY 1) AS sr_no, emp_cd
FROM   mytable

您可以使用row_number()分析函数

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM