ROW_NUMBER vs rank

Hello

I don't see the difference between the analytic function rank() and row_number()
Please, can you explain?

Thank you

SQL RANK() versus ROW_NUMBER():
http://StackOverflow.com/questions/7747327/SQL-rank-versus-row-number

select dense_rank() over (partition by b order by a) "dr",
             rank() over (partition by b order by a) "rk",
       row_number() over (partition by b order by a) "rn",
              "a", "b"
from some_table;

        DR         RK         RN          A B
---------- ---------- ---------- ---------- ----------
         1          1          1          4 #
         1          1          2          4 #
         1          1          3          4 #
         2          4          4          8 #
         2          4          5          8 #
         1          1          1          2 *
         1          1          2          2 *
         2          3          3          3 *
         2          3          4          3 *
         3          5          5          5 *
         3          5          6          5 *
         3          5          7          5 *

--source: http://www.adp-gmbh.ch/ora/sql/analytical/dense_rank_vs_rank_vs_row_number.html

Tags: Database

Similar Questions

  • Difference between ROW_NUMBER and RANK

    Hello

    Just for information, is there any significant difference in the behavior of other ROW_NUMBER, RANK, and then in a case of equal?
    As far as I know, GRADE rate two rows as eqaul in a case of ex aequo, while ROW_NUMBER treated as different with unique running serial number provided for each record.

    Here is an example of code that I have worked on:

    For each GRADE:
    WITH T AS (
     SELECT 'EMP1' NAME, 1000 SAL FROM DUAL
     UNION
     SELECT 'EMP2', 200 FROM DUAL
     UNION 
     SELECT 'EMP3', 500 FROM DUAL
     UNION
     SELECT 'EMP4', 1000 FROM DUAL
    
    )
    SELECT NAME,
           SAL,
           RANK() OVER (ORDER BY SAL DESC ) "rank"
    FROM   T
    ORDER BY SAL ;
    Output is went like:
    NAME SAL rank 
    EMP2 200 4 
    EMP3 500 3 
    EMP1 1000 1 
    EMP4 1000 1 
    While with
    ROW_NUMBER
    WITH T AS (
     SELECT 'EMP1' NAME, 1000 SAL FROM DUAL
     UNION
     SELECT 'EMP2', 200 FROM DUAL
     UNION 
     SELECT 'EMP3', 500 FROM DUAL
     UNION
     SELECT 'EMP4', 1000 FROM DUAL
    
    )
    SELECT NAME,
           SAL,
           ROW_NUMBER() OVER (ORDER BY SAL DESC ) "rank"
    FROM   T
    ORDER BY SAL;
    outout comes as
    NAME SAL rank 
    EMP2 200 4 
    EMP3 500 3 
    EMP1 1000 1 
    EMP4 1000 2 

    Yes, with row_number, I meant he would describe not any analytic property of a column but would generate a unique number for the entire file. Windowing clause would just serve as the basis of numbering.

    ROW_NUMBER does not take into account the event of tie, but numbers lines in the output.

    "Why EMP1 is given ROW_NUMBER 1 as EMP4 as 2?
    Because the numbering has been done for the classified documents. Check the below query, the rank function is commented out and gives the correct numbering output

    WITH T AS (
     SELECT 'EMP1' NAME, 1000 SAL FROM DUAL
     UNION
     SELECT 'EMP2', 200 FROM DUAL
     UNION
     SELECT 'EMP3', 500 FROM DUAL
     UNION
     SELECT 'EMP4', 1000 FROM DUAL
    
    )
    SELECT NAME,
           SAL,
           --RANK() OVER (ORDER BY sal DESC ) "rank",
           --DENSE_RANK() OVER (ORDER BY SAL DESC ) "DENSE_rank",
           ROW_NUMBER() OVER (ORDER BY 1) "ROW_NUMBER"
    FROM   T;
    
    NAME        SAL ROW_NUMBER
    ---- ---------- ----------
    EMP1       1000          1
    EMP2        200          2
    EMP3        500          3
    EMP4       1000          4
    
  • Performance of row_number, rank and dense_rank

    Hello

    Nature of my data is such that using row_number, rank and dense_rank give the same results. Which is faster?

    Hello

    Tested with my own set of data and performance are as follows.

    DENSE_RANK (faster) > rank > row_number (slower)

  • Problem using RANK() over (partition of)

    Hello

    I have a select statement that selects a single record among the many recordings sorted by date!

    SELECT the isin code, id, mkt, CTL, TM

    Of

    (SELECT isin, id, mkt, CTL, TM,

    Rank() OVER (partition by isin arrested by ASC TM) RNK

    CORD)

    where "CPOS" = id

    and "SSEFN" = mkt

    AND rnk = 1

    By this statement, I will get a single record (rnk = 1 so it select the first record)

    But the problem is that whenever there is only one record (qualifying) it does not appear in the result set.

    And I also need this record! My goal is always a single record , no matter how they are!

    I joined record a sample and also the script table here!

    SE0001ACPCNESSSSS2013-04-16 06:55:37
    SE0005ACPCNESSSSS2013-11-04 06:57:16

    CREATE TABLE cord

    (

    ISIN VARCHAR2 (13 BYTE) NOT NULL,

    ID VARCHAR2 (33 BYTE),

    VARCHAR2 (20 BYTE) TM,

    TMMILL NUMBER (13).

    TMMILLREC NUMBER (13).

    VARCHAR2 (49 BYTE) NM,

    MKT VARCHAR2 (11 BYTE) NOT NULL,

    CTL VARCHAR2 (4 BYTE)

    )

    ;

    Hello

    Louis wrote:

    Hello

    I have a select statement that selects a single record among the many recordings sorted by the timestamp!

    Whenever you have a problem, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.
    Explain, using specific examples, how you get these results from these data.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002

    Remember, RANK may return the same value for multiple lines in the same partition.

    If you have exactly 1 for each value in each partition, use ROW_NUMBER instead of RANK.

  • one-to-many selfjoin, delete records with the same rank or a substitution

    Sorry for my poor choice of the title of the discussion, feel free to suggest me a more relevant


    I rewrote for clarity and as a result of the FAQ post.


    Version of DB


    I use Oracle10g Enterprise 10.2.0.1.0 64-bit


    Tables involved

    CREATE TABLE wrhwr (
    wr_id INTEGER PRIMARY KEY,
    
    eq_id VARCHAR2(50) NULL,
    date_completed DATE NULL,
    status VARCHAR2(20) NOT NULL,
    pmp_id VARCHAR2(20) NOT NULL,
    description VARCHAR2(20) NULL);
    
    


    Examples of data


    INSERT into wrhwr  VALUES (1,'MI-EXT-0001',date'2013-07-16','Com','VER-EXC','Revisione')
    INSERT into wrhwr VALUES  (2,'MI-EXT-0001',date'2013-07-01','Com','VER-EXC','Verifica')
    INSERT into wrhwr  VALUES (3,'MI-EXT-0001',date'2013-06-15','Com','VER-EXC','Revisione')
    INSERT into wrhwr  VALUES (4,'MI-EXT-0001',date'2013-06-25','Com','VER-EXC','Verifica')
    INSERT into wrhwr  VALUES (5,'MI-EXT-0001',date'2013-04-14','Com','VER-EXC','Revisione')
    INSERT into wrhwr  VALUES (6,'MI-EXT-0001',date'2013-04-30','Com','VER-EXC','Verifica')
    INSERT into wrhwr  VALUES (7,'MI-EXT-0001',date'2013-03-14','Com','VER-EXC','Collaudo')
    
    


    Query used

    SELECT *
      FROM (SELECT eq_id,
                   date_completed,
                   RANK ()
                   OVER (PARTITION BY eq_id
                         ORDER BY date_completed DESC NULLS LAST)
                      rn
              FROM wrhwr
             WHERE     status != 'S'
                   AND pmp_id LIKE 'VER-EX%'
                   AND description LIKE '%Verifica%') table1,
           (SELECT eq_id,
                   date_completed,       
                   RANK ()
                   OVER (PARTITION BY eq_id
                         ORDER BY date_completed DESC NULLS LAST)
                      rn
              FROM wrhwr
             WHERE     status != 'S'
                   AND pmp_id LIKE 'VER-EX%'
                   AND description LIKE '%Revisione%') table2,
           (SELECT eq_id,
                   date_completed,            
                   RANK ()
                   OVER (PARTITION BY eq_id
                         ORDER BY date_completed DESC NULLS LAST)
                      rn
              FROM wrhwr
             WHERE     status != 'S'
                   AND pmp_id LIKE 'VER-EX%'
                   AND description LIKE '%Collaudo%') table3
     WHERE     table1.eq_id = table3.eq_id
           AND table2.eq_id = table3.eq_id
           AND table1.eq_id = table2.eq_id
    
    

    The above query is intended to selfjoin wrhwr table 3 times in order to have for each line:

    • eq_id;
    • date of the completion of a verification type work request for this eq_id (aka table1);
    • date completion of a line (aka table2) type wr for this eq_id;
    • date of completion of a type wr Collaudo (aka table3) for this eq_id;

    A separate eq_id:

    • can have different completion of many requests for work (wrhwr records) with dates or date of completion (date_completed NULL column).
    • in a date range can have all types of wrhwr ('verification', 'Line', 'Problem'), or some of them (e.g. audit, line but not Collaudo, Collaudo but not verification and line, etc.);
    • must not repeat the substrings in the description;
    • (eq_id, date_completed) are not unique but (eq_id, date_completed, description, pmp_id) must be unique;

    Expected results

    Using data from the example above, I expect this output:

    eq_id, table1.date_completed, table2.date_completed, table3.date_completed

    MI-ext-001,2013-07-01,2013-07-16,2013-03-14 <- to this eq_id table3 doesn't have 3 lines but only 1. I would like to repeat the value most in the rankings in table 3 for each line of output

    MI-ext-001,2013-07-01,2013-06-15,2013-03-14 <-I don't want this line of table1 and table2 with both 3 lines match must be in terms of grade (1st, 1st) (2nd, 2nd) (3rd, 3rd)

    MI-ext-001,2013-06-25,2013-06-15,2013-03-14 <-2nd table1 joined the 2nd row from table2

    MI-ext-001,2013-04-30,2013-04-14, 2013-03-14 <-1 table1, table2 rank rank 1, 1st rank table3

    In the syntax of vector style, tuple expected output should be:

    IX = ranking of the i - th of tableX

    (i1, i2, i3) IF EXISTS a rank i - th line in each table

    ON THE OTHER

    (i1, b, b)

    where b is the first available lower ranking of the table2, or NULL if there isn't any line of lower rank.

    Clues?

    With the query, I am unable to delete the lines "spurius.

    I think a solution based on analytical functions such as LAG() and LEAD(), using ROLLUP() or CUBE(), using nested queries, but I would find a solution elegant, simple, fast, and easy to maintain.


    Thank you

    Hello

    Sorry, it's still not quite clear what you are asking.

    This becomes the desired resutls of the sample data you posted:

    WITH got_r_type AS

    (

    SELECT eq_id, date_completed

    CASE

    Description WHEN LIKE '% Collaudo %' THEN 'C '.

    Description WHEN AS 'Line %' THEN 'R '.

    Description WHEN AS 'Verification %' THEN 'V '.

    END AS r_type

    OF wrhwr

    Situation WHERE! = s "

    AND pmp_id LIKE '% WORM - EX'

    )

    got_r_num AS

    (

    SELECT eq_id, date_completed, r_type

    , ROW_NUMBER () OVER (PARTITION BY eq_id, r_type)

    ORDER BY date_completed DESC NULLS LAST

    ) AS r_num

    OF got_r_type

    WHERE r_type IS NOT NULL

    )

    SELECT eq_id

    LAST_VALUE (MIN (CASE WHEN r_type THEN date_completed END = ' V')

    IGNORES NULL VALUES

    ) OVER (PARTITION BY eq_id

    ORDER BY r_num

    ) AS audit

    LAST_VALUE (MIN (CASE WHEN r_type = 'R' THEN date_completed END)

    IGNORES NULL VALUES

    ) OVER (PARTITION BY eq_id

    ORDER BY r_num

    ) AS line

    LAST_VALUE (MIN (CASE WHEN r_type = 'C' THEN date_completed END)

    IGNORES NULL VALUES

    ) OVER (PARTITION BY eq_id

    ORDER BY r_num

    ) AS collauda

    OF got_r_num

    GROUP BY eq_id

    r_num

    ORDER BY eq_id

    r_num

    ;

    I guess the description can have (at most) only substrings target, in other words, you can't have a line like this:

    INSERT into (1,'MI-EXT-0001',date'2013-07-16','Com','VER-EXC','Revisione VALUES wrhwr and audit");

    In addition, you said the combination (eq_id, date_comepleted) is not unique, that it is y no example of this in your sample data.  What results would you if, in addition to this line (who did the validation):

    INSERT into wrhwr VALUES (7,'MI-EXT-0001',date'2013-03-14','Com','VER-EXC','Collaudo');

    the following line is also?

    INSERT into wrhwr VALUES (97,'MI-EXT-0001',date'2013-03-14','Com','VER-EXCFUBAR','Collaudo');

    .

    You could do a self-join instead of GROUP BY, but I suspect it will be much less effective.  You can use FULL OUTER JOIN, since you do not know what r_types was that r_nums.

  • Please explain ranking and score?

    I am familiar with basic SQL and PL/SQL.  I met the following clause in a query, I do not understand:

    SELECT...

    T1.valueA,

    T1. DateValue,

    T1.valueB,

    T1.dateValue_2,

    IsCurrent

    (SELECT
    Decode (ROW_NUMBER () over)
    (order by T1.dateValue ASC T1.valueA partition), 1, DECODE (sign (T1.dateValue - sysdate), 1, ' n 'Y'), 'n') as IsCurrent
    "N" as IsCancellable
    dense_rank () over (partition T1.valueA order by T1.valueB desc, T1.dateValue desc, T1.dateValue_2 desc) as rank

    T1 etc...

    If it's just a way of ordering within groups.

    No - no groups. The partition clause is about the creation of 'sets', not groups. The word 'group' has a very specific meaning in SQL and it is important that it be not used incorrectly or people are going to get confused.

    When the data are combined the result often put less than rows as there are in the source data because data are aggregated.

    When the data is partitioned any line is destroyed or created but data "sets" are produced.

  • Rank of computed column is not work

    Hello PL/SQL gurus and experts.


    I use Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64-bit Production version
    I have table-

    drop table t3;
    create table t3(Pat_NM,Hospital,Test_Range,Total,LyTotal) as select
    'Andy','Batra','> 10 Mph','20000','20000' from dual union all select
    'Andy','Fortis','1-3 Mph','24500','20000' from dual union all select
    'Andy','Max','5-10 Mph','10600','20000' from dual union all select
    'Andy','Max','5-10 Mph','22500','20000' from dual union all select
    'Andy','Aashiana','5-10 Mph','110600','20000' from dual union all select
    'Andy','Amar','5-10 Mph','34800','20000' from dual union all select
    'Andy','Max','5-10 Mph','600','20000' from dual union all select
    'Andy','Columbia','< 1 Mph','27700','20000' from dual union all select
    'Andy','Nimhans','< 1 Mph','50000','20000' from dual union all select
    'Andy','Meenam','< 1 Mph','11000','20000' from dual union all select
    'Andy','Meeran','5-10 Mph','24625','20000' from dual union all select
    'Andy','Mnagamani','> 10 Mph','12000','20000' from dual union all select
    'Andy','Murari','> 10 Mph','20600','20000' from dual union all select
    'Andy','Triveni','5-10 Mph','16500','20000' from dual union all select
    'Cindy','Batra','5-10 Mph','14700','20000' from dual union all select
    'Cindy','Max','< 1 Mph','170000','20000' from dual union all select
    'Cindy','Apollo Medical Centre','> 10 Mph','19000','20000' from dual union all select
    'Cindy','MLal','1-3 Mph','22600','20000' from dual union all select    
    'Cindy','Columbia','< 1 Mph','28900','20000' from dual union all select
    'Cindy','Asian','1-3 Mph','27900','20000' from dual union all select
    'Cindy','Mahagun','< 1 Mph','28700','20000' from dual union all select
    'Cindy','Manipal','< 1 Mph','29040','20000' from dual union all select
    'Cindy','Prestige','< 1 Mph','12700','20000' from dual union all select
    'Cindy','A.G.M.','< 1 Mph','97800','20000' from dual union all select
    'Cindy','Shobha','< 1 Mph','700','20000' from dual union all select
    'Cindy','Aashiana','5-10 Mph','23450','20000' from dual union all select
    'Cindy','Amar','1-3 Mph','21325','20000' from dual union all select
    'Cindy','Childs Trust','5-10 Mph','22775','20000' from dual union all select
    'Cindy','Crescent ','< 1 Mph','20025','20000' from dual;
    

    I have an Anlzd rank as this logic folloing calculation-
    Ranking ((Cy Yr Tot *Factor-Prev Yr Tot) /Prev year Tot)

    and it does not correct-

    Currently I use the following DML-

    select Pat_NM,
    decode(grouping(Test_Range), 0, Test_Range, 'Total') "Test Range",
    Hospital,
    SUM (Total) "Total",
    SUM (AnlzdTotal) "Anlzd Total",
    SUM (LyTotal) "Last Yr Total",
    decode(grouping(Test_Range), 0, max(rank), null) Rank,
    decode(grouping(Test_Range), 0, max(lyrank), null) "Anlzd Rank"
    from
    (Select Pat_NM,
             Hospital,
             Test_Range,
             SUM (Total) Total,
      SUM (AnlzdTot) AnlzdTotal,
      SUM (LyTotal) LyTotal,
             DENSE_RANK ()
                OVER (PARTITION BY test_range || pat_nm ORDER BY SUM (LyTotal) DESC)
                AS LYRANK,
     DENSE_RANK ()
                OVER (PARTITION BY test_range || pat_nm ORDER BY SUM (Total) DESC)
                AS RANK,
             ROW_NUMBER ()
                OVER (PARTITION BY test_range || pat_nm ORDER BY SUM (Total) DESC)
                AS rk
    from
    (
    SELECT Pat_NM,
             Hospital,
             Test_Range,
             SUM (Total) Total,
      SUM (LyTotal) LyTotal,
      sum (Total) *1.85 AnlzdTot,
      (sum (Total * 1.85 - LyTotal)/nullif(sum(LyTotal),0)) AnlzdTotRank         
        FROM t3
    GROUP BY Pat_NM, Hospital, Test_Range
    )
    group by Pat_NM, Hospital, Test_Range)
    group by grouping sets((Pat_NM, Test_Range,Hospital),())
    order by Pat_NM,Test_Range, Rank;
    
    

    Output data, I'm getting-

    PAT_N Test Ran HOSPITAL                   Total Anlzd Total Last Yr Total       RANK Anlzd Rank
    ----- -------- --------------------- ---------- ----------- ------------- ---------- ----------
    Andy  1-3 Mph  Fortis                     24500       45325         20000          1          1
    Andy  5-10 Mph Aashiana                  110600      204610         20000          1          2
    Andy  5-10 Mph Amar                       34800       64380         20000          2          2
    Andy  5-10 Mph Max                        33700       62345         60000          3          1
    Andy  5-10 Mph Meeran                     24625    45556.25         20000          4          2
    Andy  5-10 Mph Triveni                    16500       30525         20000          5          2
    Andy  < 1 Mph  Nimhans                    50000       92500         20000          1          1
    Andy  < 1 Mph  Columbia                   27700       51245         20000          2          1
    Andy  < 1 Mph  Meenam                     11000       20350         20000          3          1
    Andy  > 10 Mph Murari                     20600       38110         20000          1          1
    Andy  > 10 Mph Batra                      20000       37000         20000          2          1
    PAT_N Test Ran HOSPITAL                   Total Anlzd Total Last Yr Total       RANK Anlzd Rank
    ----- -------- --------------------- ---------- ----------- ------------- ---------- ----------
    Andy  > 10 Mph Mnagamani                  12000       22200         20000          3          1
    Cindy 1-3 Mph  Asian                      27900       51615         20000          1          1
    Cindy 1-3 Mph  MLal                       22600       41810         20000          2          1
    Cindy 1-3 Mph  Amar                       21325    39451.25         20000          3          1
    Cindy 5-10 Mph Aashiana                   23450     43382.5         20000          1          1
    Cindy 5-10 Mph Childs Trust               22775    42133.75         20000          2          1
    Cindy 5-10 Mph Batra                      14700       27195         20000          3          1
    Cindy < 1 Mph  Max                       170000      314500         20000          1          1
    Cindy < 1 Mph  A.G.M.                     97800      180930         20000          2          1
    Cindy < 1 Mph  Manipal                    29040       53724         20000          3          1
    Cindy < 1 Mph  Columbia                   28900       53465         20000          4          1
    PAT_N Test Ran HOSPITAL                   Total Anlzd Total Last Yr Total       RANK Anlzd Rank
    ----- -------- --------------------- ---------- ----------- ------------- ---------- ----------
    Cindy < 1 Mph  Mahagun                    28700       53095         20000          5          1
    Cindy < 1 Mph  Crescent                   20025    37046.25         20000          6          1
    Cindy < 1 Mph  Prestige                   12700       23495         20000          7          1
    Cindy < 1 Mph  Shobha                       700        1295         20000          8          1
    Cindy > 10 Mph Apollo Medical Centre      19000       35150         20000          1          1
          Total                              925640     1712434        580000
    

    I want to - data

    PAT_N Test Ran HOSPITAL                   Total Anlzd Total Last Yr Total       RANK Anlzd Rank
    ----- -------- --------------------- ---------- ----------- ------------- ---------- ----------
    Andy  1-3 Mph  Fortis                     24500       45325         20000          1          1
    Andy  5-10 Mph Aashiana                  110600      204610         20000          1          1
    Andy  5-10 Mph Amar                       34800       64380         20000          2          2
    Andy  5-10 Mph Max                        33700       62345         60000          3          5
    Andy  5-10 Mph Meeran                     24625    45556.25         20000          4          3
    Andy  5-10 Mph Triveni                    16500       30525         20000          5          4
    Andy  < 1 Mph  Nimhans                    50000       92500         20000          1          1
    Andy  < 1 Mph  Columbia                   27700       51245         20000          2          2
    Andy  < 1 Mph  Meenam                     11000       20350         20000          3          3
    Andy  > 10 Mph Murari                     20600       38110         20000          1          1
    Andy  > 10 Mph Batra                      20000       37000         20000          2          2
    PAT_N Test Ran HOSPITAL                   Total Anlzd Total Last Yr Total       RANK Anlzd Rank
    ----- -------- --------------------- ---------- ----------- ------------- ---------- ----------
    Andy  > 10 Mph Mnagamani                  12000       22200         20000          3          3
    Cindy 1-3 Mph  Asian                      27900       51615         20000          1          1
    Cindy 1-3 Mph  MLal                       22600       41810         20000          2          2
    Cindy 1-3 Mph  Amar                       21325    39451.25         20000          3          3
    Cindy 5-10 Mph Aashiana                   23450     43382.5         20000          1          1
    Cindy 5-10 Mph Childs Trust               22775    42133.75         20000          2          2
    Cindy 5-10 Mph Batra                      14700       27195         20000          3          3
    Cindy < 1 Mph  Max                       170000      314500         20000          1          1
    Cindy < 1 Mph  A.G.M.                     97800      180930         20000          2          2
    Cindy < 1 Mph  Manipal                    29040       53724         20000          3          3
    Cindy < 1 Mph  Columbia                   28900       53465         20000          4          4
    PAT_N Test Ran HOSPITAL                   Total Anlzd Total Last Yr Total       RANK Anlzd Rank
    ----- -------- --------------------- ---------- ----------- ------------- ---------- ----------
    Cindy < 1 Mph  Mahagun                    28700       53095         20000          5          5
    Cindy < 1 Mph  Crescent                   20025    37046.25         20000          6          6
    Cindy < 1 Mph  Prestige                   12700       23495         20000          7          7
    Cindy < 1 Mph  Shobha                       700        1295         20000          8          8
    Cindy > 10 Mph Apollo Medical Centre      19000       35150         20000          1          9
          Total                              925640     1712434        580000
    

    Thanks in advance for all your efforts, time and valuable feedback.

    Thanks for the comments FrankKulash, I checked that it was due to the "last nulls' to be added in the following lines.

    DENSE_RANK)

    COURSES (PARTITION BY test_range | pat_nm ORDER OF SUM (LyTotal) / / DESC nulls last)

    AS LYRANK,

    DENSE_RANK)

    COURSES (PARTITION BY test_range | pat_nm ORDER OF SUM (Total) / / DESC nulls last)

    IN THE RANK,.

    DENSE_RANK)

    COURSES (PARTITION BY test_range | pat_nm ORDER OF SUM (AnlzdTotRank) / / DESC nulls last)

    AS ANLZDRANK,

    ROW_NUMBER)

    COURSES (PARTITION BY test_range | pat_nm ORDER OF SUM (Total) / / DESC)

    AS rk

  • Restart rank comes not correct based on column group

    Hello PL/SQL gurus and experts.


    I use Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64-bit Production version
    I have table-

    drop table t2;
    create table t2(Hospital,Test_Range,Total) as select
    'Batra','> 10 Mph','20000' from dual union all select
    'Fortis','1-3 Mph','24500' from dual union all select
    'Max','5-10 Mph','10600' from dual union all select
    'Columbia','< 1 Mph','27700' from dual union all select
    'Nimhans','< 1 Mph','50000' from dual union all select
    'Meenam','< 1 Mph','11000' from dual union all select
    'Meeran','5-10 Mph','24625' from dual union all select
    'Mnagamani','> 10 Mph','12000' from dual union all select
    'Murari','> 10 Mph','20600' from dual union all select
    'Triveni','5-10 Mph','16500' from dual union all select
    'Batra','5-10 Mph','14700' from dual union all select
    'Max','< 1 Mph','170000' from dual union all select
    'Apollo Medical Centre','> 10 Mph','19000' from dual union all select
    'MLal','1-3 Mph','22600' from dual union all select    
    'Columbia','< 1 Mph','28900' from dual union all select
    'Asian','1-3 Mph','27900' from dual union all select
    'A.G.M.','< 1 Mph','22700' from dual union all select
    'Aashiana','5-10 Mph','23450' from dual union all select
    'Amar Hospital','1-3 Mph','21325' from dual union all select
    'Childs Trust','5-10 Mph','22775' from dual union all select
    'Crescent ','< 1 Mph','20025' from dual;
    

    I use the following DML-

    select Test_Range "Test_Range",
    SUM (Total) "Total",
    decode(grouping(Hospital), 0, Hospital, 'Total') "Hospital",
    decode(grouping(Hospital), 0, max(rank), null) Rank
    from
    (
    SELECT Hospital,
             Test_Range,
             SUM (Total) Total,
             DENSE_RANK ()
                OVER (PARTITION BY test_range || Hospital ORDER BY SUM (Total) DESC)
                AS RANK,
             ROW_NUMBER ()
                OVER (PARTITION BY test_range || Hospital ORDER BY SUM (Total) DESC)
                AS rk
        FROM t2
    GROUP BY Hospital, Test_Range
    )
    group by grouping sets((Hospital, Test_Range),())
    order by Test_Range, Rank;
    

    Get the following output-

    Test_Ran      Total Hospital                    RANK
    -------- ---------- --------------------- ----------
    1-3 Mph       24500 Fortis                         1
    1-3 Mph       21325 Amar Hospital                  1
    1-3 Mph       22600 MLal                           1
    1-3 Mph       27900 Asian                          1
    5-10 Mph      14700 Batra                          1
    5-10 Mph      22775 Childs Trust                   1
    5-10 Mph      10600 Max                            1
    5-10 Mph      23450 Aashiana                       1
    5-10 Mph      16500 Triveni                        1
    5-10 Mph      24625 Meeran                         1
    < 1 Mph       20025 Crescent                       1
    Test_Ran      Total Hospital                    RANK
    -------- ---------- --------------------- ----------
    < 1 Mph       50000 Nimhans                        1
    < 1 Mph       56600 Columbia                       1
    < 1 Mph       11000 Meenam                         1
    < 1 Mph      170000 Max                            1
    < 1 Mph       22700 A.G.M.                         1
    > 10 Mph      12000 Mnagamani                      1
    > 10 Mph      20000 Batra                          1
    > 10 Mph      20600 Murari                         1
    > 10 Mph      19000 Apollo Medical Centre          1
                 610900 Total
    21 rows selected.
    

    While I am looking for following output-

    Test_Ran      Total Hospital                    RANK
    -------- ---------- --------------------- ----------
    1-3 Mph       27900 Asian                          1
    1-3 Mph       24500 Fortis                         2
    1-3 Mph       22600 MLal                           3
    1-3 Mph       21325 Amar Hospital                  4
    5-10 Mph      24625 Meeran                         1
    5-10 Mph      23450 Aashiana                       2
    5-10 Mph      22775 Childs Trust                   3
    5-10 Mph      16500 Triveni                        4
    5-10 Mph      14700 Batra                          5
    5-10 Mph      10600 Max                            6
    < 1 Mph      170000 Max                            1
    < 1 Mph       56600 Columbia                       2
    < 1 Mph       50000 Nimhans                        3
    Test_Ran      Total Hospital                    RANK
    -------- ---------- --------------------- ----------
    < 1 Mph       22700 A.G.M.                         4
    < 1 Mph       20025 Crescent                       5
    < 1 Mph       11000 Meenam                         6
    > 10 Mph      20600 Murari                         1
    > 10 Mph      20000 Batra                          2
    > 10 Mph      19000 Apollo Medical Centre          3
    > 10 Mph      12000 Mnagamani                      4
                 610900 Total
    21 rows selected.
    

    Kindly help me and thanks in advance for your time, effort and assistance

    Hello

    If you want a separate set of numbers (1, 2, 3,...) for each value of test_range, without seeking to hold account of the hospital, then the PARTITON OF only test_range, not to mention the hospital.

  • Rank within a group based on Total, start it over 1 in each group

    Hello PL/SQL gurus and experts.


    I use Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64-bit Production version
    I have table-

    Like this??

    SELECT Pat_NM,

    Hospital,

    Test_Range,

    SUM (Total),

    DENSE_RANK)

    COURSES (PARTITION BY test_range | pat_nm ORDER OF SUM (Total) / / DESC)

    IN THE RANK,.

    ROW_NUMBER)

    COURSES (PARTITION BY test_range | pat_nm ORDER OF SUM (Total) / / DESC)

    AS rk

    THE t2

    After HAVING Pat_NM = 'Andy '.

    GROUP OF Pat_NM, hospital, Test_Range;

    See you soon,.

    Manik.

  • Ranking on consecutive dates

    Hi guys,.

    I've been struggling with this for a few days already and I tried to implement different solutions, that I was able to get the internet search and apply some learned approeaches from this forum on similar questions, but perhaps than if I put that question here someone knows exactly how best to manage :

    So, I have a table like this:

    with payments as
    (
    select '1' as ID, '20130331' as DateR, 'Not_paid' as Status from dual
    union
    select '1' as ID, '20130430' as DateR, 'Paid' as Status from dual
    union
    select '1' as ID, '20130531' as DateR, 'Not_paid' as Status from dual
    union
    select '2' as ID, '20130331' as DateR, 'Not_paid' as Status from dual
    union
    select '2' as ID, '20130430' as DateR, 'Not_paid' as Status from dual
    union
    select '3' as ID, '20130331' as DateR, 'Paid' as Status from dual
    union
    select '3' as ID, '20130430' as DateR, 'Paid' as Status from dual
    union
    select '3' as ID, '20130531' as DateR, 'Paid' as Status from dual
    )


    And I try to get the amount of the current row when of a user. Now it begins to count whenever the previous payments had been paid or it s the first payment.

    I tried to get dense rank, that gives me output:

    select ID, dater, status, dense_rank() over (partition by ID, status order by dater asc) rnk from payments


    But I need to get something like this:

    ID DATER STATUS RNK
    1   20130331  Not_paid  1
    1   20130430  Paid  1
    1   20130531  Not_Paid  1
    2   20130331  Not_paid  1
    2   20130430  Not_paid  2
    3   20130331  Paid  1
    3   20130430  Paid  2
    3   20130531  Paid  3


    Such as if I want to get the max (rank) to check how the defaults a user has currently I get this ID has 1 unpayment, ID 2 two consecutive unpayments, and ID 3 0 unpayments. This is because I have to take into account the user as brewed on the fourth consecutive non-payment.

    Hello

    Here's one way:

    WITH got_grp_num AS

    (

    SELECT p

    ROW_NUMBER () OVER (PARTITION BY ID.

    ORDER BY date

    )

    -ROW_NUMBER () OVER (PARTITION BY id, status)

    ORDER BY date

    ) AS grp_num

    PAYMENTS p

    )

    SELECT id, date, status

    , ROW_NUMBER () OVER (PARTITION BY id, status, grp_num)

    ORDER BY date

    ) AS rnk

    OF got_grp_num

    ORDER BY id, date

    ;

    For an explanation of the technique of Difference sets used here, see

    https://forums.Oracle.com/forums/thread.jspa?threadID=2302781 and/or

    https://forums.Oracle.com/forums/thread.jspa?threadID=2303591

    Moreover, store date information in a column of sting is simply asking for trouble.  Use a DATE column.

  • DENSE_RANK and find the top three rows return regardless of their rank.

    Oracle Database 10 g Express Edition Release 10.2.0.1.0 - product

    Hello

    I have the following problem;

    Dense_rank using I've categorized the trees in a plot of height.

    Now I need to filter the first three records by parcel based on the position of the trees, it's complicated by the fact that sometimes two or three of the three tallest trees have the same height.

    Records that I want to use for the parcel number 90001 are tree numbers 4, 5 and 3.

    Records that I want to use for the parcel number 31000 are tree numbers 4, 5, and 3, but rather the number 2 shaft is included.
    select distinct plot_number
         , tree_number
         , tree_height
         , dense_rank() over (partition by plot_number order by tree_height desc)ht_rank
      from tst_rank
      order by plot_number, ht_rank asc
    ;
    
    PLOT_NUMBER TREE_NUMBER TREE_HEIGHT HT_RANK
    ----------- ----------- ----------- -------
          31000           4          10       1 
          31000           5          10       1 
          31000           3           3       2 
          31000           2           2       3 
          31000           1           1       4 
          90001           5          20       1 
          90001           4          10       2 
          90001           3           3       3 
          90001           2           2       4 
          90001           1           1       5 
    
     10 rows selected 
    The dense rank function allows me to classify the trees, but then it has accomplished its purpose. The following query is an example of what I want to do - but - I need to access the 3 early accounts after the rankings were made, so the following query is not going to use the collation.
    select plot_number
         , max(decode(ht_rank, '1', tree_height*100)) ht_1
         , max(decode(ht_rank, '2', tree_height*100)) ht_2
         , max(decode(ht_rank, '3', tree_height*100)) ht_3
    from (select distinct plot_number
               , tree_number
               , tree_height
               , dense_rank() over (partition by plot_number order by tree_height desc)ht_rank
            from tst_rank
           order by plot_number, ht_rank asc
          )txt  
     group by plot_number
    ;
    
    PLOT_NUMBER HT_1 HT_2 HT_3
    ----------- ---- ---- ----
          31000   10    3    2 
          90001   20   10    3 
    But I want to see;
    PLOT_NUMBER HT_1 HT_2 HT_3
    ----------- ---- ---- ----
          31000   10   10    3 
          90001   20   10    3 
    Anyone have some ideas of what I could do to get there?

    See you soon
    Ben


    Create script to replicate my problem;
    -- drop table tst_rank;
    
    CREATE TABLE TST_RANK
    ( PLOT_NUMBER NUMBER
    , TREE_NUMBER NUMBER
    , TREE_HEIGHT NUMBER
    );
    
    INSERT INTO TST_RANK (PLOT_NUMBER, TREE_NUMBER, TREE_HEIGHT) VALUES ('31000','1', '1');
    INSERT INTO TST_RANK (PLOT_NUMBER, TREE_NUMBER, TREE_HEIGHT) VALUES ('31000','2', '2');
    INSERT INTO TST_RANK (PLOT_NUMBER, TREE_NUMBER, TREE_HEIGHT) VALUES ('31000','3', '3');
    INSERT INTO TST_RANK (PLOT_NUMBER, TREE_NUMBER, TREE_HEIGHT) VALUES ('31000','4', '10');
    INSERT INTO TST_RANK (PLOT_NUMBER, TREE_NUMBER, TREE_HEIGHT) VALUES ('31000','5', '10');
    
    INSERT INTO TST_RANK (PLOT_NUMBER, TREE_NUMBER, TREE_HEIGHT) VALUES ('90001','1', '1');
    INSERT INTO TST_RANK (PLOT_NUMBER, TREE_NUMBER, TREE_HEIGHT) VALUES ('90001','2', '2');
    INSERT INTO TST_RANK (PLOT_NUMBER, TREE_NUMBER, TREE_HEIGHT) VALUES ('90001','3', '3');
    INSERT INTO TST_RANK (PLOT_NUMBER, TREE_NUMBER, TREE_HEIGHT) VALUES ('90001','4', '10');
    INSERT INTO TST_RANK (PLOT_NUMBER, TREE_NUMBER, TREE_HEIGHT) VALUES ('90001','5', '20');
    
    COMMIT;
    
    /*
    
    select distinct plot_number
         , tree_number
         , tree_height
         , dense_rank() over (partition by plot_number order by tree_height desc)ht_rank
      from tst_rank
      order by plot_number, ht_rank asc
    ;
    
    select plot_number
         , max(decode(ht_rank, '1', tree_height)) ht_1
         , max(decode(ht_rank, '2', tree_height)) ht_2
         , max(decode(ht_rank, '3', tree_height)) ht_3
    from (select distinct plot_number
               , tree_number
               , tree_height
               , dense_rank() over (partition by plot_number order by tree_height desc)ht_rank
            from tst_rank
           order by plot_number, ht_rank asc
          )txt  
     group by plot_number
    ;
    
    */
    Published by: benton on 9 October 2012 14:45

    Use ROW_NUMBER

    SQL> select plot_number,
      2         max(decode(rn,1,tree_height)) h1,
      3         max(decode(rn,2,tree_height)) h2,
      4         max(decode(rn,3,tree_height)) h3
      5  from(
      6  select plot_number,tree_height,
      7         row_number() over(partition by plot_number order by tree_height desc) rn
      8  from tst_rank)
      9  group by plot_number;
    
    PLOT_NUMBER         H1         H2         H3
    ----------- ---------- ---------- ----------
          31000         10         10          3
          90001         20         10          3
    
  • Ranking query

    Hi all

    Could someone help me please?

    I need rank/order one set of data by date and period type (blocktype) records of worker groups. When the blocktype "Ind (individual)", I need to increment the row by 1. Where the type is "Cont (inuous)", I don't want to increment the row until the next "IND record." This is how I can get then the start/end dates of min/max for this rank number.

    I don't know if it's clear, but it's really obvious what should I do with the attached SQL below (see column 'Désirée'). I'm guessing that rank and dense_rank is the way to go, but I don't know what is the combination of the columns of the partition/drive by?

    If anyone could help'd me smile. :)

    Thank you.



    create the table mytable
    (pid varchar (10),)
    StartDate date,
    date EndDate,
    BlockType varchar (4),
    desired varchar (1));

    insert into mytable (pid, startdate, enddate, blocktype, desired) values ("A123", to_date('23/10/2007','DD/MM/RRRR'), to_date('05/11/2007','DD/MM/RRRR'), 'ind', 1);
    insert into mytable (pid, startdate, enddate, blocktype, desired) values ("A123", to_date('16/11/2007','DD/MM/RRRR'), to_date('19/11/2007','DD/MM/RRRR'), 'ind', 2);
    insert into mytable (pid, startdate, enddate, blocktype, desired) values ("A123", to_date('26/11/2007','DD/MM/RRRR'), to_date('25/04/2008','DD/MM/RRRR'), "cont", 3);
    insert into mytable (pid, startdate, enddate, blocktype, desired) values ("A123", to_date('25/04/2008','DD/MM/RRRR'), to_date('28/08/2008','DD/MM/RRRR'), "cont", 3);
    insert into mytable (pid, startdate, enddate, blocktype, desired) values ("A123", to_date('01/04/2009','DD/MM/RRRR'), to_date('09/09/2009','DD/MM/RRRR'), 'ind', 4);
    insert into mytable (pid, startdate, enddate, blocktype, desired) values ("A123", to_date('01/05/2010','DD/MM/RRRR'), to_date('03/05/2010','DD/MM/RRRR'), "cont", 5);
    insert into mytable (pid, startdate, enddate, blocktype, desired) values ("A123", to_date('03/05/2010','DD/MM/RRRR'), to_date('19/11/2010','DD/MM/RRRR'), "cont", 5);
    insert into mytable (pid, startdate, enddate, blocktype, desired) values ("A123", to_date('19/11/2010','DD/MM/RRRR'), to_date('06/02/2011','DD/MM/RRRR'), "cont", 5);
    insert into mytable (pid, startdate, enddate, blocktype, desired) values ("A123", to_date('29/04/2011','DD/MM/RRRR'), to_date('14/11/2011','DD/MM/RRRR'), 'ind', 6);

    Published by: user9363122 on October 28, 2011 16:35

    Hello

    Little Penguin says:
    In this case, I have to get the start date and end date for the period of the child to education where the code start is (s, B) and the end code! = X.

    In the sample data you posted, it looks like a begions period again whenever startcode is either a ' or 'B' and that endcode has nothing to do with it:

    insert into mytable3 (PID, StartDate, Enddate, StartCode EndCode, desired) values ('A657', to_date('11/12/2008','DD/MM/RRRR'), to_date('24/12/2008','DD/MM/RRRR'),' to, 'X', 1);
    insert into mytable3 (PID, StartDate, Enddate, StartCode EndCode, desired) values ('A657', to_date('24/12/2008','DD/MM/RRRR'), to_date('22/05/2009','DD/MM/RRRR'), 'P', 'X', 1);
    insert into mytable3 (PID, StartDate, Enddate, StartCode EndCode, desired) values ('A657', to_date('22/05/2009','DD/MM/RRRR'), to_date('25/11/2009','DD/MM/RRRR'), 'P', 'E', 1);
    insert into mytable3 (PID, StartDate, Enddate, StartCode EndCode, desired) values ('A657', to_date('04/05/2010','DD/MM/RRRR'), to_date('17/06/2010','DD/MM/RRRR'),' to, 'X', 2);
    insert into mytable3 (PID, StartDate, Enddate, StartCode EndCode, desired) values ('A657', to_date('17/06/2010','DD/MM/RRRR'), to_date('19/07/2010','DD/MM/RRRR'), 'B', 'E', 3);
    insert into mytable3 (PID, StartDate, Enddate, StartCode EndCode, desired) values ('A657', to_date('29/09/2010','DD/MM/RRRR'), to_date('23/11/2010','DD/MM/RRRR'),', 'E', 4);
    insert into mytable3 (PID, StartDate, Enddate, StartCode EndCode, desired) values ('A657', to_date('14/01/2011','DD/MM/RRRR'), to_date('20/04/2011','DD/MM/RRRR'), 'P', 'E', 4);
    insert into mytable3 (PID, StartDate, Enddate, StartCode EndCode, desired) values ('A657", to_date('20/04/2011','DD/MM/RRRR'), null, 'B', null, 5);

    In the above data, periods 2 and 5 do not contain an enddate = 'E '.

    That's why I need assign a dense_rank

    It doesn't have to be a DENSE_RANK, t - it? As long as you get good results, you will not care if DENSE_RANK or some other function produces these results, right?

    based on the first startup code for the first end code, the second beginning at the end of the second code code, the third code beginning at the end of the third code etc. sorted by start date.
    ... I thought that it might be possible using row_number()?

    Good idea, but it could be even better ways to do this.
    The ROW_NUMBER analytic function has all rows .
    The analytical COUNT function counts only certain lines . In the following query, there are only lines where startcode in ('B', the from '):

    SELECT       m.*
    ,       COUNT ( CASE
                          WHEN  startcode IN ('B', 'S')
                    THEN  1
                    ELSE  NULL     -- This is the default, but you can explicitly say it if you want to
                   END
                 ) OVER ( PARTITION BY  pid
                           ORDER BY      startdate
                     )     AS period
    FROM       mytable3     m
    ORDER BY  pid
    ,            startdate
    ;
    

    To find the startdate and enddate in the whole of the period, this in a subquery and then use the analytical MIN and MAX functions, like this:

    WITH     got_period     AS
    (
         SELECT     m.*
         ,     COUNT ( CASE
                             WHEN  startcode IN ('B', 'S')
                      THEN  1
                          ELSE  NULL     -- This is the default, but you can explicitly say it if you want to
                         END
                       )     OVER ( PARTITION BY  pid
                                ORDER BY          startdate
                        )     AS period
         FROM     mytable3     m
    )
    SELECT       p.*
    ,       MIN (startdate) OVER (PARTITION BY period)     AS period_startdate
    ,       MAX (enddate)     OVER (PARTITION BY period)     AS period_enddate
    FROM       got_period  p
    ORDER BY  pid
    ,            startdate
    ;
    

    Output:

    PID   STARTDATE  ENDDATE    S E DE     PERIOD PERIOD_STA PERIOD_END
    ----- ---------- ---------- - - -- ---------- ---------- ----------
    A657  11/12/2008 24/12/2008 S X 1           1 11/12/2008 25/11/2009
    A657  24/12/2008 22/05/2009 P X 1           1 11/12/2008 25/11/2009
    A657  22/05/2009 25/11/2009 P E 1           1 11/12/2008 25/11/2009
    A657  04/05/2010 17/06/2010 S X 2           2 04/05/2010 17/06/2010
    A657  17/06/2010 19/07/2010 B E 3           3 17/06/2010 19/07/2010
    A657  29/09/2010 23/11/2010 S E 4           4 29/09/2010 20/04/2011
    A657  14/01/2011 20/04/2011 P E 4           4 29/09/2010 20/04/2011
    A657  20/04/2011            B   5           5 20/04/2011
    

    Why do I say you must use a subquery to get period? Discuss.

  • Combine several values of rank in a line like comma delimeted string

    Hello

    I have a requirement to combine several values of rank in a line as the comma delimeted string as below

    INDEX_NAME COLUMN_NAME POSITION_COLONNE
    EMP_EMAIL_UK EMAIL 1
    EMP_EMP_ID_PK EMPLOYE_ID 1
    EMP_DEPARTMENT_IX DEPARTMENT_ID 1
    JOB_ID EMP_JOB_IX 1
    EMP_MANAGER_IX MANAGER_ID 1
    EMP_NAME_IX LAST_NAME 1
    EMP_NAME_IX FIRST_NAME 2
    I write in a SQL and the output I need is, for example EMP_NAME_IX LAST_NAME, FIRST_NAME.

    I can't write any function as well.

    http://www.Oracle-base.com/articles/Misc/StringAggregationTechniques.php

    SELECT index_name,
           ltrim(sys_Connect_by_path(column_name, ','), ',') column_names
      FROM (select index_name,
                   column_name,
                   row_number() over(partition by index_name order by column_position) rn
              from all_ind_columns
             WHERE table_owner = 'HR')
     WHERE connect_by_isleaf = 1
     START WITH rn = 1
    CONNECT BY PRIOR rn = rn - 1
           and prior index_name = index_name
    
  • Rank function in responses

    Hello

    I have to produce a graphic barcode with number of Projects(Y axis), Phase(X axis) project. For example, x axis should have labels such as Phase 1, Phase 2 etc, and in each phase, the number of projects should be the bar. The problem is the number of projects should be counted only in the phase where the "Recorded_date" is max.

    In the database, I can use something similar to this
    Select * from)
    SELECT project, phase_id, recorded_date,
    ROW_NUMBER() over (PARTITION BY project ORDER BY recorded_date desc) as rnk
    of MAIN.project_detail) a
    where rnk = 1;

    How to implement this? Please let me know if there is another way. I don't want to make changes to the RPD, if I can accomplish this in answers.

    Thank you for your time and your help.

    So, in your case,.

    Select * from)
    SELECT project, phase_id, recorded_date,
    ROW_NUMBER() over (PARTITION BY project ORDER BY recorded_date desc) as rnk
    of MAIN.project_detail) a
    where rnk = 1;

    would be done by the statement "ROW_NUMBER() OVER (PARTITION BY project ORDER BY recorded_date desc) as rnk"
    turns

    rank (recorded_date by project)

    in the function (fx) column.

  • How to reset the rank or rownum in BI publisher

    Hello
    I have a scenario where I have to print the details of the itinerary and the list of teachers teaching course.so if a college is to have 3 courses then there will be three pages, each page with a detail of the course with the list of teachers of this course.
    to do this, I have a point of view, which means that I have a request for details of the course with the details of the teacher.

    so in my model, I have a column (No. BIL) for teacher details that counts the number of teachers.i have used rank for numbering. But the problem is that the number is continuous, i.e. for the course of one if there are 2 teachers then enter invoice number is 1 and 2. but when it goes to page 2 of course 2 and this course is to have 3 teachers, the Bill starts from 3. but it should start from one. How to solve this problem.

    You can try the following query

    Select ROW_NUMBER() OVER (ORDER BY ASC NULLS LAST rownum course_id partition) AS BIL,.
    a.* from course_det where CRS_COLLEGE_ID = '101875'

    This should give the numbering, as expected. I assumed column courses either course_id.

Maybe you are looking for