create sequence of numbers on grouped column in Oracle

user3468000

Consider below table with column a,b,c.

a   b   c
3   4   5
3   4   5
6   4   1
1   1   8
1   1   8
1   1   0
1   1   0

I need a select statement to get below output. i.e. increment column 'rn' based on group of column a,b,c.

a   b   c   rn
3   4   5   1
3   4   5   1
6   4   1   2
1   1   8   3
1   1   8   3
1   1   0   4
1   1   0   4
EJ Egyed

You can use the DENSE_RANK analytic function to get a unique ID for each combination of A, B, and C. Just note that if a new value is inserted into the table, the IDs of each combination of A, B, and C will shift and may not be the same.

Query

WITH
    my_table (a, b, c)
    AS
        (SELECT 3, 4, 5 FROM DUAL
         UNION ALL
         SELECT 3, 4, 5 FROM DUAL
         UNION ALL
         SELECT 6, 4, 1 FROM DUAL
         UNION ALL
         SELECT 1, 1, 8 FROM DUAL
         UNION ALL
         SELECT 1, 1, 8 FROM DUAL
         UNION ALL
         SELECT 1, 1, 0 FROM DUAL
         UNION ALL
         SELECT 1, 1, 0 FROM DUAL)
SELECT t.*, DENSE_RANK () OVER (ORDER BY b desc, c desc, a) as rn
  FROM my_table t;

Result

   A    B    C    RN
____ ____ ____ _____
   3    4    5     1
   3    4    5     1
   6    4    1     2
   1    1    8     3
   1    1    8     3
   1    1    0     4
   1    1    0     4

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Oracle sequence - skipped numbers

From Dev

Create an array with a sequence of numbers in bash

From Dev

how to create '\' before a sequence of numbers?

From Dev

Create column with grouped values based on another column

From Dev

Create Oracle sequence using ExecuteNonQuery

From Dev

Create a sequence from vectors of start and end numbers

From Dev

how create a sequence of strings with different numbers in R

From Dev

MySQL Create View with Sequence Numbers for Each Group

From Dev

Return column with running sequence number Oracle

From Dev

Return column with running sequence number Oracle

From Dev

Create Sequence if it not exists using oracle sql developer

From Dev

Using Oracle's CREATE SEQUENCE as secure random

From Dev

How to query the distribution (grouped by value range) of a column value in Oracle?

From Dev

SQL Server: how to create sequence number column

From Dev

create new dataframe grouped by one column and new columns

From Dev

Select a column where the row has a maximum value in a column grouped over a third column in Oracle

From Java

SQL Server: Create sequence column based on a non-distinct column

From Dev

How to create a sequence of events (column values) per some other column?

From Dev

Create a sequence of unique observations by group with dplyr and create a difference in months column

From Dev

Create a dynamic programming algorithm to compute the fibonacci sequence using tetranacci numbers

From Dev

How to create an ascending sequence of text/numbers for html slideshow

From Dev

how to create sequence of numbers in same digit with a character in R

From Dev

bash - how do I use 2 numbers on a line to create a sequence

From Dev

Create a dataframe with random numbers in each column

From Dev

VIM create column of REPEATING increasing numbers

From Dev

How To create Sequence in Oracle Developer 6i

From Dev

How can I create a pivot table indexed on a column with duplicate entries that should be grouped by values of another column?

From Dev

How can I create a pivot table indexed on a column with duplicate entries that should be grouped by values of another column?

From Dev

Create new column that increments based on row sequence in R

Related Related

  1. 1

    Oracle sequence - skipped numbers

  2. 2

    Create an array with a sequence of numbers in bash

  3. 3

    how to create '\' before a sequence of numbers?

  4. 4

    Create column with grouped values based on another column

  5. 5

    Create Oracle sequence using ExecuteNonQuery

  6. 6

    Create a sequence from vectors of start and end numbers

  7. 7

    how create a sequence of strings with different numbers in R

  8. 8

    MySQL Create View with Sequence Numbers for Each Group

  9. 9

    Return column with running sequence number Oracle

  10. 10

    Return column with running sequence number Oracle

  11. 11

    Create Sequence if it not exists using oracle sql developer

  12. 12

    Using Oracle's CREATE SEQUENCE as secure random

  13. 13

    How to query the distribution (grouped by value range) of a column value in Oracle?

  14. 14

    SQL Server: how to create sequence number column

  15. 15

    create new dataframe grouped by one column and new columns

  16. 16

    Select a column where the row has a maximum value in a column grouped over a third column in Oracle

  17. 17

    SQL Server: Create sequence column based on a non-distinct column

  18. 18

    How to create a sequence of events (column values) per some other column?

  19. 19

    Create a sequence of unique observations by group with dplyr and create a difference in months column

  20. 20

    Create a dynamic programming algorithm to compute the fibonacci sequence using tetranacci numbers

  21. 21

    How to create an ascending sequence of text/numbers for html slideshow

  22. 22

    how to create sequence of numbers in same digit with a character in R

  23. 23

    bash - how do I use 2 numbers on a line to create a sequence

  24. 24

    Create a dataframe with random numbers in each column

  25. 25

    VIM create column of REPEATING increasing numbers

  26. 26

    How To create Sequence in Oracle Developer 6i

  27. 27

    How can I create a pivot table indexed on a column with duplicate entries that should be grouped by values of another column?

  28. 28

    How can I create a pivot table indexed on a column with duplicate entries that should be grouped by values of another column?

  29. 29

    Create new column that increments based on row sequence in R

HotTag

Archive