Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to rank based on certain column value oracle SQL

select * from 
(
  SELECT NORM_VERSION, NORM_FULL_VERSION, NORM_PRODUCT_VALUE, LIFECYCLE_TYPE, START_DATE,
         rank() OVER (PARTITION BY NORM_VERSION, NORM_FULL_VERSION,
                                   NORM_PRODUCT_VALUE,
                                   CASE WHEN LIFECYCLE_TYPE ='internal'
                                        then 1 end
                      ORDER BY SYS_UPDATED_ON DESC) as dest_rank
  FROM LIFECYCLE
)
where dest_rank = 1 ;

how to rank ,if lifecycle type is internal then rank it 1 then increment the ranking from the grouped columns in the above query.

from below screenshot I am trying to rank internal 1 then publisher 2

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You appear to want to move the CASE expression to the ORDER BY clause (rather than having different lifecycle_types in different partitions):

SELECT *
FROM   (
  SELECT NORM_VERSION,
         NORM_FULL_VERSION,
         NORM_PRODUCT_VALUE,
         LIFECYCLE_TYPE,
         START_DATE,
         RANK() OVER (
           PARTITION BY
             NORM_VERSION,
             NORM_FULL_VERSION,
             NORM_PRODUCT_VALUE
           ORDER BY
             CASE WHEN LIFECYCLE_TYPE ='internal' THEN 1 ELSE 2 END,
             SYS_UPDATED_ON DESC
         ) as dest_rank
  FROM LIFECYCLE
)
WHERE  dest_rank = 1 ;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading