Sql 访问查询中的If/Then
我需要一些在Access中构造If/Then查询的帮助。我希望根据持卡人、审批人和对账人列填充新的注释字段:Sql 访问查询中的If/Then,sql,ms-access,if-this-then-that,Sql,Ms Access,If This Then That,我需要一些在Access中构造If/Then查询的帮助。我希望根据持卡人、审批人和对账人列填充新的注释字段: If only Cardholder is filled: New Comments field = A If only Reconciler is filled: New Comments field = B If only Approver is filled: New Comments field = C If Cardholder & Reconciler filled:
If only Cardholder is filled: New Comments field = A
If only Reconciler is filled: New Comments field = B
If only Approver is filled: New Comments field = C
If Cardholder & Reconciler filled: New Comments field =D
If Cardholder & Approver filled: New Comments field = E
If Cardholder & Reconciler & Approver filled: New Comments field = F
If Reconciler & Approver filled: New Comments field = G
您能帮忙吗?因为您有三个字段,每个字段可以是
Null
或notnull
有23=8种可能的情况
以下将说明每种情况:
NewComments:
IIf([Cardholder] Is Not Null And [Reconciler] Is Not Null And [Approver] Is Not Null,"F",
IIf([Cardholder] Is Not Null And [Reconciler] Is Not Null,"D",
IIf([Cardholder] Is Not Null And [Approver] Is Not Null,"E",
IIf([Reconciler] Is Not Null And [Approver] Is Not Null,"G",
IIf([Cardholder] Is Not Null,"A",
IIf([Reconciler] Is Not Null,"B",
IIf([Approver] Is Not Null,"C",
"H"
)
)
)
)
)
)
)
最终结果“H”
对应于所有三个字段都为Null
的情况
显然,可以反转逻辑,从每个语句中删除
Not
,但我已经根据您提供的示例构造了该语句。因为您有三个字段,每个字段可以是Null
或notnull
,有23=8种可能的情况
以下将说明每种情况:
NewComments:
IIf([Cardholder] Is Not Null And [Reconciler] Is Not Null And [Approver] Is Not Null,"F",
IIf([Cardholder] Is Not Null And [Reconciler] Is Not Null,"D",
IIf([Cardholder] Is Not Null And [Approver] Is Not Null,"E",
IIf([Reconciler] Is Not Null And [Approver] Is Not Null,"G",
IIf([Cardholder] Is Not Null,"A",
IIf([Reconciler] Is Not Null,"B",
IIf([Approver] Is Not Null,"C",
"H"
)
)
)
)
)
)
)
最终结果“H”
对应于所有三个字段都为Null
的情况
显然,可以颠倒逻辑,从每个语句中删除
而不是,但我已根据您提供的示例构造了该语句。在MS Access中,开关是处理多个条件的更简单方法:
select . . .
switch([Cardholder] Is Not Null And [Reconciler] Is Not Null And [Approver] Is Not Null, "F",
[Cardholder] Is Not Null And [Reconciler] Is Not Null, "D",
[Cardholder] Is Not Null And [Approver] Is Not Null, "E",
[Reconciler] Is Not Null And [Approver] Is Not Null, "G",
[Cardholder] Is Not Null, "A",
[Reconciler] Is Not Null, "B",
[Approver] Is Not Null, "C",
1=1, "H"
)
在MS Access中,开关
是处理多种情况的更简单方法:
select . . .
switch([Cardholder] Is Not Null And [Reconciler] Is Not Null And [Approver] Is Not Null, "F",
[Cardholder] Is Not Null And [Reconciler] Is Not Null, "D",
[Cardholder] Is Not Null And [Approver] Is Not Null, "E",
[Reconciler] Is Not Null And [Approver] Is Not Null, "G",
[Cardholder] Is Not Null, "A",
[Reconciler] Is Not Null, "B",
[Approver] Is Not Null, "C",
1=1, "H"
)
您可以忽略上面列出的代码格式--这些只是我希望用于If/Then查询的参数您可以忽略上面列出的代码格式--这些只是我希望用于If/Then查询的参数