Search code examples
oracle-databaseautonumber

Auto Number by Grouping Column in Oracle


Is there any oracle statement for my problem.

Eg. TableA has 2 columns : columnA and columnB

ColumnA    ColumnB
-------------------
 A         WA
 A         WA
 B         WA
 B         WA
 C         WA
 A         CA
 A         CA
 B         CA
 B         CA
 C         CA

What I want is : AutoNumber Group by ColumnA

Auto  ColumnA  ColumnB
-----------------------
1     A        WA
2     A        WA
3     A        CA
4     A        CA
1     B        WA
2     B        WA
3     B        CA
4     B        CA
1     C        WA
2     C        CA

Solution

  • You can use ROW_NUMBER function for this. Group by your columnA and specify some order by clause.

    SELECT ROW_NUMBER () OVER (PARTITION BY columnA ORDER BY columnB) as "auto",
           columnA, columnB
      FROM table;