简体   繁体   中英

Return column with running sequence number Oracle

My simple query returns data like this:

SELECT column1, column2 FROM table1

COLUMN1   COLUMN2
-------   -------
CA         A
CA         B
CB         C
CB         D

I want to return column3 with these values (for same COLUMN1 value, I want to return same sequence number):

COLUMN3
-------
1
1
2
2

You can use analytic function DENSE_RANK .

SELECT column1, 
       column2,
       DENSE_RANK() OVER(ORDER BY column1) as "column3"
 FROM table1

See the following for some examples - oracle-base.com/articles/misc/rank-dense-rank-first-last-analytic-functions.php#dense_rank

Try this query,

Select column1, column2, 
       dense_rank() over (order by column1) as column3 
from table1;

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