[英]Using If statement in On statement
I am presenting a summary of transactions by customer/account number. 我按客户/账号提出交易摘要。 Transactions have two possible components, hardware or software I want the user to either get a presentation that has the total for the account, or hardware and software totals presented separately.
事务有两个可能的组件,硬件或软件,我希望用户获得具有帐户总数的演示文稿,或者单独提供的硬件和软件总计。
Currently the only option provided to the user is to get them separately. 目前,提供给用户的唯一选择是单独获取它们。 To accomplish this I have a subquery with a join whose on statement currently reads
为了实现这一点,我有一个带有连接的子查询,其on语句当前是读取的
on tb3.account_nbr=tb4.account_nbr and tb3.trans_type=tb4.trans_type
Eliminating the trans_type evaluation of the the statement returns the total for all transactions on an account. 取消对该语句的trans_type评估将返回帐户上所有事务的总计。
I have added a checkbox on the form that indicates their preference for either getting the total or the two components separately. 我在表单上添加了一个复选框,表示他们偏好分别获取总计或两个组件。 The resulting variable (@get_trans) value, is either "1" for show as separate totals by transaction type or "0" for summing all transaction types and showing as single total.
结果变量(@get_trans)值为“1”表示为按事务类型单独的总计,或“0”表示所有事务类型的总和并显示为单个总计。
It would make sense that I could change the above to an if statement for the solution but it isn't working 有意义的是,我可以将上面的内容更改为解决方案的if语句,但它无法正常工作
on tb3.account_nbr=tb4.account_nbr and if(@get_trans=1,tb3.trans_type=tb4.trans_type,"")
Any help would be appreciated. 任何帮助,将不胜感激。 Thanks, Jim
谢谢,吉姆
The problem is probably the else
clause. 问题可能是
else
子句。 The empty string will evaluate to 0
in this context -- and be treated as false. 在此上下文中,空字符串将计算为
0
- 并将其视为false。 That causes the entire on
condition to be false. 这使得整个
on
情况是假的。
However, you can write the logic without the if
: 但是,您可以在不使用
if
情况下编写逻辑:
on tb3.account_nbr = tb4.account_nbr and
(@get_trans <> 1 or tb3.trans_type = tb4.trans_type)
Note: this version assumes that @get_trans
does not take on a NULL
value. 注意:此版本假定
@get_trans
不采用NULL
值。 That is easily handled: 这很容易处理:
on tb3.account_nbr = tb4.account_nbr and
(@get_trans <> 1 or @get_trans is null or tb3.trans_type = tb4.trans_type)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.