简化 Case When If then 语句

Simplify Case When If then statement

我们的 MIS 提供商向我提供了以下 CASE WHEN 声明。我正在考虑简化这个,因为我不是专家,所以我越容易阅读,我就越有机会理解和学习。

当前报表:

,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

这就是我能够写出的内容,从中我知道可以工作的部分是 WHEN SopOrderItem.SoiProcessMethod = 0。在这一点上我不明白这个说法。据我所知,它是说当 SopOrderItem.SoiProcessMethod 为 0 时,然后检查 SopProduct.SopPrePostPaid 是否也为 0,如果两者都为真,则 "Replenishment Pay on Replenishment",如果不是,则 "Replenishment Pay on Delivery"。

有什么方法可以简化语句,使我更容易理解。

我现在有以下语句,但这导致我的查询失败并需要很长时间才能刷新:

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

就我个人而言,我会将 CASE 和 IF 分开而不是将它们合并。但这只会提高可读性。如果这是您依赖这些值的某种查询的一部分,我认为您需要重新考虑它,因为它不会非常有效。

(您需要确保这段代码确实给出了相同的结果——没有数据和 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.

您也可以将其设为仅 IF:

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.