Search code examples
sqlexcelopenedgecase-whenprogress-db

Simplify Case When If then statement


I have been provided the following CASE WHEN statement from our MIS provider. I am looking at simplifying this as im no expert so the easier it is for me to read the more chance i have to understand and learn.

Current statement:

,CASE SopOrderItem.SoiProcessMethod:WHEN 0 
THEN IF SopOrderItem.SoiReplenishmentOrder 
THEN DO: IF SopProduct.SopPrePostPaid = 0 
THEN ASSIGN lv-ProcessMethod = "Replenishment Pay on Replenishment".
ELSE ASSIGN lv-ProcessMethod = "Replenishment Pay on Delivery".
END.
ELSE lv-processmethod = "Call Off":U.
WHEN 1 THEN lv-processmethod = "On Demand":U.
WHEN 2 THEN lv-processmethod = "Personalised":U.
END CASE

This is what i have been able to write from which i know that works the part i am stuck on is WHEN SopOrderItem.SoiProcessMethod = 0. At this point i do not understand the statement. From what i can tell in English it is saying when SopOrderItem.SoiProcessMethod is 0 then check to see if SopProduct.SopPrePostPaid is also 0 if both are true then "Replenishment Pay on Replenishment" if not then "Replenishment Pay on Delivery".

Is there a way i can simplify the statement to make it easier for me to understand.

I have the below statement at the moment but this has caused my query to fall over and take ages to refresh:

CASE WHEN SopOrderItem.SoiProcessMethod = 1 THEN 'On Demand'
WHEN SopOrderItem.SoiProcessMethod = 2 THEN 'Personalised'
WHEN SopOrderItem.SoiProcessMethod = 0 AND SopProduct.SopPrePostPaid = 0 THEN "Replenishment Pay on Replenishment"
WHEN SopOrderItem.SoiProcessMethod = 0 AND SopProduct.SopPrePostPaid <> 0 THEN "Replenishment Pay on Delivery"
ELSE 'Call Off'
END

Solution

  • Personally I would separate the CASE and IF and not merge them. But that would only improve readability. If this is part of some kind of query where you depend on these values I think you need to rethink it since it wont be very efficient.

    (You need to make sure that this code really gives the same result - it's kind of hard without data and without definitions of SopOrderItem + SopProduct).

    CASE SopOrderItem.SoiProcessMethod:
        WHEN 0 THEN DO :
            IF SopOrderItem.SoiReplenishmentOrder THEN DO:
                IF SopProduct.SopPrePostPaid = 0 THEN
                    ASSIGN lv-ProcessMethod = "Replenishment Pay on Replenishment".
                ELSE 
                    ASSIGN lv-ProcessMethod = "Replenishment Pay on Delivery".
            END.
            ELSE lv-processmethod = "Call Off":U.
        END.
    
        WHEN 1 THEN lv-processmethod = "On Demand":U.
        WHEN 2 THEN lv-processmethod = "Personalised":U.
    END CASE.
    

    You could also make it IF-only:

    IF SopOrderItem.SoiProcessMethod = 0 THEN DO :
        IF SopOrderItem.SoiReplenishmentOrder THEN DO:
            IF SopProduct.SopPrePostPaid = 0 THEN
                ASSIGN lv-ProcessMethod = "Replenishment Pay on Replenishment".
            ELSE 
                ASSIGN lv-ProcessMethod = "Replenishment Pay on Delivery".
        END.
        ELSE lv-processmethod = "Call Off":U.
    END.
    ELSE IF SopOrderItem.SoiProcessMethod = 1 THEN lv-processmethod = "On Demand":U.
    ELSE IF SopOrderItem.SoiProcessMethod = 2 THEN lv-processmethod = "Personalised":U.