Refactoring a query criteria with an IIF against a field also containing an IIF (1 Viewer)

InstructionWhich7142

Registered User.
Local time
Today, 06:54
Joined
Feb 24, 2010
Messages
199
I've got a query that's evolved over the years, the field in question has an IIF statement resulting in in one of two dates (A & B) and the criteria I now need to apply to it is an IIF that also results in one of two dates (D & E), C and F are a boolean field I need to check as that affects which date is used

Code:
Query Expression: iif(A>B or C, A, B)

Code:
Criteria Expression: <>iif(D>E or F, D, E)

I'd like to convert this to 2 fields with simple criteria not containing IIFs

The reason for this is Access' Query Optimiser automatically refactors the IIfs into a mess of statements that don't give correct results

Using two queries gives the correct result so I know the logic is "correct" but I'd like to understand the process of breaking this logic down for future usage as well

I understand that I can convert
Code:
(A>B or C) <> (D>E or F)

Into multiple lines like:

(A>B) and F

C and (D>)

C and F

That bit is like mathematical algebra and you can "show your working" however I'm not sure how to handle the "Result" part of the IF in the same way, I'm not sure how to "expand the brackets" for that bit

I sort of know the full statement for the first one would be

Code:
A>B and F and A <> D

But I'm struggling to explain why
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:54
Joined
Oct 29, 2018
Messages
21,454
Hi. Just a guess, but maybe something like:
Code:
...WHERE ((A>B OR C) AND (D>E OR F) AND A<>D) OR ((A<B OR NOT C) AND (D<E OR NOT F) AND B<>E)
 
Last edited:

InstructionWhich7142

Registered User.
Local time
Today, 06:54
Joined
Feb 24, 2010
Messages
199
Yea something like that, and the kinda alternative half of each one but yea, that's the kind of thing,

So I guess my question is for working stuff out like this in future is there some way to write an IIF in a truth table or how do I do algebra with something like this to explain
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:54
Joined
Oct 29, 2018
Messages
21,454
Yea something like that, and the kinda alternative half of each one but yea, that's the kind of thing,

So I guess my question is for working stuff out like this in future is there some way to write an IIF in a truth table or how do I do algebra with something like this to explain
No clue, really. Sorry...
 

vba_php

Forum Troll
Local time
Today, 00:54
Joined
Oct 6, 2019
Messages
2,880
is there some way to write an IIF in a truth table or how do I do algebra with something like this to explain
it may be easier to use data in a table for the purposes of doing comparisons but i would guess the query might get long or you would end up using too many stacked queries? might be best just to stick with IIF(). I've never used a truth table so I wouldn't know either:


https://en.wikipedia.org/wiki/Truth_table
 

Users who are viewing this thread

Top Bottom