iif语句访问SQL过多

时间:2018-12-19 20:56:19

标签: sql ms-access

我有一个很大的嵌套iif语句。我有16个不同的字段,需要根据它们的初始值分配1或0的值。在这里:

select
  iif(overlimitexception = "Yes",0,1) as OverLimit,
  iif(CashOutageIdentified = "Yes",0,1) AS CashOutage,
  iif(MissingAuditIdentified = "Yes",0,1) AS MissingAudit,
  iif(NightDropObserved = "Yes",0,1) AS NightDrop, 
  iif(SecurityTestComplete = "No",0,1) AS SecurityTest,
  iif(CashPatternIdentified = "Yes",0,1) AS CashPattern,
  iif(AllClearChange = "No",0,1) AS AllClear,
  iif(RobberyKitReview = "No",0,1),
  iif(EvacPlanReview = "No",0,1),
  iif(KeyComboIssue = "Yes",0,1) AS KeyCombo,
  iif(DualControlIssue = "Yes",0,1) AS DualControl,
  iif(TaIssue = "Yes", 0,1) AS TA,
  iif(CleanDeskIssue = "Yes",0,1) AS CleanDesk,
  iif(MonthlyOpsIssue = "Yes",0,1) AS MonthlyOps,
  iif(OverShortIssue = "Yes",0,1) AS OverShort,
  iif(CashTargetIssue = "Yes",0,1) AS CashTarget 
From [ROM Acknowledgement]

如果我只处理一个领域,但可以解决16个问题,那么我可以轻松解决此问题。有没有更好的方法?感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

如果您对iif过敏,以下是一种可能的选择:

select
    1+(overlimitexception = "Yes") as OverLimit,
    1+(CashOutageIdentified = "Yes") AS CashOutage,
    1+(MissingAuditIdentified = "Yes") AS MissingAudit,
    1+(NightDropObserved = "Yes") AS NightDrop, 
    1+(SecurityTestComplete = "No") AS SecurityTest,
    1+(CashPatternIdentified = "Yes") AS CashPattern,
    1+(AllClearChange = "No") AS AllClear,
    1+(RobberyKitReview = "No"),
    1+(EvacPlanReview = "No"),
    1+(KeyComboIssue = "Yes") AS KeyCombo,
    1+(DualControlIssue = "Yes") AS DualControl,
    1+(TaIssue = "Yes") AS TA,
    1+(CleanDeskIssue = "Yes") AS CleanDesk,
    1+(MonthlyOpsIssue = "Yes") AS MonthlyOps,
    1+(OverShortIssue = "Yes") AS OverShort,
    1+(CashTargetIssue = "Yes") AS CashTarget 
from 
    [ROM Acknowledgement]

答案 1 :(得分:0)

假设在提到的字段中只有“否”和“是”值,您可以这样写

  1 - eval(overlimitexception) as OverLimit,
  -eval(SecurityTestComplete) AS SecurityTest,

或者更快一点

  3 - len(overlimitexception) as OverLimit,
  -2 + len(SecurityTestComplete) AS SecurityTest,