case语句

beq87vna  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(367)

我有这样的要求,我必须模拟一个excel公式到oraclesql中。由于excel是非常初级的,所有的东西都放在里面,但是sql是有条理的,必须保持一致。
这是excel(业务类别)中的公式:

  1. =IF([@[Order Days]]="","",
  2. IF(OR([@business type]="Probation Touch Point",[@business type]="Referral",
  3. [@business type]="Mystery Shopping"),IF([@[Order Days]]>45,"Allowed","Not Allowed"),
  4. IF(OR(AND([@business status]="Assigned",[@[Order Days]]>60),
  5. AND([@business status]="Pre-Review",[@[Order Days]]>60),
  6. AND([@business status]="In-Review",[@[site]]="Location",[@[Order Days]]>44),
  7. AND([@business status]="In-Review",[@[site]]="Headquarters",[@[Order Days]]>74)),"Permitted",
  8. IF([@business status]="Post-Review",IF([@business review]=0,IF([@[Order Days]]>44,"Allowed","Not
  9. Allowed"),
  10. IF(OR(AND([@[site]]="Headquarters",[@[Order Days]]>90),
  11. AND([@[site]]="Location",[@[Order Days]]>60)),"Allowed","Not Allowed")),"Not Allowed"))))

由于其突出,有多种 else 在一个单一的公式和它的目的。
我的sql公式:

  1. case when order_days is null then null
  2. when business_type like '%Touch%' or business_type = 'Referral'
  3. or business_type like 'Myster%' and order_days > 45 then 'Allowed'
  4. when (business_status = 'Assigned' and order_days > 60) or
  5. (business_status = 'Pre-Review' and order_days > 60) or
  6. (business_status = 'In-Review' and site like 'Loca%' and order_days > 44) or
  7. (business_status = 'In-Review' and site like 'Head%' and order_days > 74) then 'Permitted'
  8. when (business_status like 'Post%' or business_type = 0 or order_days > 44) then 'Allowed'
  9. when (site like 'Head%' and order_days > 90) then 'Allowed'
  10. when (site like 'Loca%' and order_days > 60) then 'Allowed' else 'Not Allowed'
  11. end as business_category

这个 sql 这个案子是徒劳的,因为它导致了错误的数字。
excel结果:

  1. Business Category Count of Rows
  2. Allowed 130
  3. Not Allowed 1122
  4. Permitted 200

sql结果:

  1. business_category Count
  2. Allowed 980
  3. Not Allowed 272
  4. Permitted 200

有人能帮忙吗!

gmol1639

gmol16391#

你把逻辑换成了 LIKE 在某些地方。因为您没有包含任何数据,所以无法确定这是否导致了您报告的某些问题,但我已恢复了与excel公式中使用的单个值的比较。此外,我认为您对“业务回顾<>0”和“业务状况<>‘事后回顾’”分支机构的解释可能有误。
以下操作应满足您的要求:

  1. case
  2. when order_days is null
  3. then null
  4. when business_type in ('Probation Touch Point', 'Referral', 'Mystery Shopping') AND order_days > 45
  5. then 'Allowed'
  6. when (business_status = 'Assigned' and order_days > 60) or
  7. (business_status = 'Pre-Review' and order_days > 60) or
  8. (business_status = 'In-Review' and site = 'Location' and order_days > 44) or
  9. (business_status = 'In-Review' and site = 'Headquarters' and order_days > 74) then 'Permitted'
  10. when (business_status = 'Post-Review' and business_review = 0 and order_days > 44) then 'Allowed'
  11. when (business_status = 'Post-Review' and business_review = 0 and order_days <= 44) then 'Not Allowed'
  12. when (business_status = 'Post-Review' and business_review <> 0 and ((site = 'Headquarters' and order_days > 90) or
  13. (site = 'Location' and order_days > 60))
  14. then 'Allowed'
  15. else 'Not Allowed'
  16. when business_status <> 'Post-Review' then 'Not Allowed'
  17. end as business_category

在将来,您可能需要对数据进行后期测试-创建表语句和插入语句以将数据放入这些表中是一个好主意-以及预期的结果。

展开查看全部

相关问题