带有多个嵌套其他的案例声明

时间:2020-06-09 21:13:58

标签: sql oracle

我有这个要求,我必须将Excel公式模拟成Oracle SQL。由于Excel非常初级,因此所有内容都包含在内,但是SQL受到限制并且必须符合要求。

这是Excel(业务类别)中的公式:

=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"))))

最引人注目的是,一个公式中有多个else并且有目的。

我的SQL公式:

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

sql案是徒劳的,因为它会导致数字错误。

Excel结果:

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

SQL结果:

business_category            Count
Allowed                       980
Not Allowed                   272
Permitted                     200

有人可以提供帮助吗?

1 个答案:

答案 0 :(得分:0)

您已在某些地方切换了使用LIKE的逻辑。因为您没有包括任何数据,所以无法确定这是否引起了您报告的某些问题,但是我已经将比较恢复为Excel公式中使用的各个值。另外,我认为您对“ business_review <> 0”和“ business_status <>'Post Review'”分支的解释可能有误。

以下应做您想做的事:

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

将来,您可能希望发布测试数据-将CREATE TABLE语句和INSERT语句放入数据到这些表中是一个好主意-以及预期的结果。