开发者

Use of Switch in MS Access SQL

开发者 https://www.devze.com 2023-02-01 04:49 出处:网络
I\'m trying to d开发者_开发技巧o a basic calculation in MS Access SQL. I have a table called Weekly, with weekly stats for each user, includes the following columns: User, Fail, Pass, Approve and Rej

I'm trying to d开发者_开发技巧o a basic calculation in MS Access SQL.

I have a table called Weekly, with weekly stats for each user, includes the following columns: User, Fail, Pass, Approve and Reject (and some unimportant id and date values).

When I perform the following query:

SELECT User, Switch(Approve>0,Pass/Approve, Approve = 0, Null) AS passed_approved_ratio, Switch(Reject>0,Fail/Reject, Reject= 0, Null) AS fail_reject_ratio
FROM Weekly

I get an #ERROR value in several resulting rows.

I've tried it with and without the second condition+value in the Switch funciton (Switch(Approve>0, Pass/Approve),

Fail, Pass, Approve and Reject all have a non-null numerical value in the Weekly table, so why don't they resolve to a Null value when Approve or Reject are 0?


In the Office help it states:

Switch evaluates all of the expressions, even though it returns only one of them. For this reason, you should watch for undesirable side effects. For example, if the evaluation of any expression results in a division by zero error, an error occurs.


The solution:

Use Iif, thus:

SELECT Iif([Approve]>0, Pass/Approve, Null) as pass_approved_ratio
FROM Weekly

That works just fine for now. I'd still like to understand why I was getting errors with Switch, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消