I have a TAble TABLE1 and having columns like ID, Status and Code
BAsed on the code priority i want the output , the priority is
SS -> RR - > TT - > AA ( these priority is not stored in any tables)
Query should first look for Approved status then we need to check for Code column Example1: ID: 2345 - This record having Approved status for all the codes like SS , AA and RR and based on the code priority SS should be pulled in the output as 2345, SS
Example2: ID: 3333- This record having Approved status for all the codes like RR and TT and based on the code priority RR should be pulled in the output as 3333, RR
ID: 4444- Eventhough this record is having Codes like SS and RR but it is status column is having value as TERMED so we need to populate the next priority in the list and output should display as 4444 TT
ID: 5555- None of the status for this ID is having Approved status all are having status as Termed so based on the priority in the output 5555,SS should be picked as this one is the priority
so output for 2345 and 5555 is same only the difference is if none of the record having approved status then only we should go for Termed - if the record is only having termed then based on priority record should be pulled
Attached the picture for reference
You may use
RANK
along with aCASE
expression for ordering:Demo