简体   繁体   中英

Case statement with Multiple Nested Else

I have this requirement where I have to emulate an Excel formula into Oracle SQL. Since Excel is very rudimentary, everything goes in it but SQL is regimented and must fall in line.

This is the formula in Excel (Business Category):

=IF([@[Order Days]]="","",

IF(OR([@business type]="Probation Touch Point",[@business type]="Referral",
[@business type]="Mystery Shopping"),IF([@[Order Days]]>45,"Allowed","Not Allowed"),

IF(OR(AND([@business status]="Assigned",[@[Order Days]]>60),
AND([@business status]="Pre-Review",[@[Order Days]]>60),
AND([@business status]="In-Review",[@[site]]="Location",[@[Order Days]]>44),
AND([@business status]="In-Review",[@[site]]="Headquarters",[@[Order Days]]>74)),"Permitted",

IF([@business status]="Post-Review",IF([@business review]=0,IF([@[Order Days]]>44,"Allowed","Not 
Allowed"),

IF(OR(AND([@[site]]="Headquarters",[@[Order Days]]>90),
AND([@[site]]="Location",[@[Order Days]]>60)),"Allowed","Not Allowed")),"Not Allowed"))))

As its conspicuous, there are multiple else within a single formula and its purposeful.

My SQL formula:

case when order_days is null then null

when business_type like '%Touch%' or business_type = 'Referral'
or business_type like 'Myster%' and order_days > 45 then 'Allowed'

when (business_status = 'Assigned' and order_days > 60) or
(business_status = 'Pre-Review' and order_days > 60) or
(business_status = 'In-Review' and site like 'Loca%' and order_days > 44) or
(business_status = 'In-Review' and site like 'Head%'  and order_days > 74) then 'Permitted'

when (business_status like 'Post%' or business_type = 0 or order_days > 44) then 'Allowed'

when (site like 'Head%'  and order_days > 90) then 'Allowed'

when (site like 'Loca%'  and order_days > 60) then 'Allowed' else 'Not Allowed'

end as business_category

The sql case is futile because it results in wrongful figures.

Excel result:

Business Category         Count of Rows
Allowed                       130
Not Allowed                   1122
Permitted                     200

SQL Result:

business_category            Count
Allowed                       980
Not Allowed                   272
Permitted                     200

Can someone please provide assistance!

You switched the logic to use LIKE in some places. Because you didn't include any data it's impossible to determine if this was causing some of the problems you reported, but I've restored the comparison to the individual values used in the Excel formula. In addition, I think your interpretation of the "business_review <> 0" and the "business_status <> 'Post Review'" branches may have been in error.

The following should do what you want:

case
  when order_days is null
    then null
  when business_type in ('Probation Touch Point', 'Referral', 'Mystery Shopping') AND order_days > 45
    then 'Allowed'
  when (business_status = 'Assigned' and order_days > 60) or
       (business_status = 'Pre-Review' and order_days > 60) or
       (business_status = 'In-Review' and site = 'Location' and order_days > 44) or
       (business_status = 'In-Review' and site = 'Headquarters' and order_days > 74) then 'Permitted'
  when (business_status = 'Post-Review' and business_review = 0 and order_days > 44) then 'Allowed'
  when (business_status = 'Post-Review' and business_review = 0 and order_days <= 44) then 'Not Allowed'
  when (business_status = 'Post-Review' and business_review <> 0 and ((site = 'Headquarters' and order_days > 90) or
                                                                      (site = 'Location' and order_days > 60))
    then 'Allowed'
    else 'Not Allowed'
  when business_status <> 'Post-Review' then 'Not Allowed'
end as business_category

In future you might want to post test data - CREATE TABLE statement(s) and INSERT statements to put data into those table(s) are a good idea - along with expected results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM