And OR (1 Viewer)

CosmaL

Registered User.
Local time
Today, 19:34
Joined
Jan 14, 2010
Messages
92
Hello friends,


i have the following syntax in my query:


WHERE (((tblMaintenanceActions.FinishedDate) Between Date() And Date()+30) AND ((tblMaintenanceActions.Completed)=False)) OR (((tblMaintenanceActions.AApproval)=False)) OR (((tblMaintenanceActions.BApproval)=False));


I want to view the next 30 days records which must be or completed, or A approved or B approved.


With this syntax, i get records more than 30 days (ex april 2020).


Any idea?
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 02:34
Joined
Jan 20, 2009
Messages
12,851
AND takes precedence over OR unless the parenthesis dictate otherwise.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:34
Joined
Oct 29, 2018
Messages
21,449
Hi. In other words, your criteria parentheses should basically resolve to something this.

...WHERE (FieldName Between Date1 And Date2) AND (Criteria1 OR Criteria2 OR Criteria3)
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 17:34
Joined
Sep 12, 2006
Messages
15,634
or even a version of this.

..WHERE (FieldName Between Date1 And Date2) AND NOT (Criteria1 AND Criteria2 AND Criteria3)

here, criteria 1 2 and 3 may be the opposite of the original. ie, sometimes it is easier to discard the full matches, rather than test for exclusions separately. so test for positives, rather than negatives.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:34
Joined
Oct 29, 2018
Messages
21,449
or even a version of this.

..WHERE (FieldName Between Date1 And Date2) AND NOT (Criteria1 AND Criteria2 AND Criteria3)

here, criteria 1 2 and 3 may be the opposite of the original. ie, sometimes it is easier to discard the full matches, rather than test for exclusions separately. so test for positives, rather than negatives.
Sometimes, yes; but sometimes, it depends.



So, be aware...
 

Attachments

  • true.png
    true.png
    5.3 KB · Views: 182

Micron

AWF VIP
Local time
Today, 12:34
Joined
Oct 20, 2018
Messages
3,478
I don't get that comment either, but I'm also not sure what the pictures are supposed to mean. About a year ago, I got deep into Boolean operators and found that they're more complicated (or at least the use of them is) than what most people seem to realize. The results in the pic make sense to me:

(True OR False OR False) will return true if any of the inputs are True

Not (True AND False AND False) - NOT will return True if the input is False. Since something cannot be( true and false and false), it is false, thus it is True that it is NOT True.

(True AND True AND True) should be obvious that this is True

Not (True AND True AND True) The expression evaluates to True, thus it is False to say the expression does NOT result in True.

IIRC, when there are more than 2 expressions and OR is involved, it is more important how the operators are grouped. I'll venture to say that one operator that you seldom see is XOR, possibly because it's not broadly understood.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:34
Joined
Oct 29, 2018
Messages
21,449
I don't get that comment either, but I'm also not sure what the pictures are supposed to mean. About a year ago, I got deep into Boolean operators and found that they're more complicated (or at least the use of them is) than what most people seem to realize. The results in the pic make sense to me:

(True OR False OR False) will return true if any of the inputs are True

Not (True AND False AND False) - NOT will return True if the input is False. Since something cannot be( true and false and false), it is false, thus it is True that it is NOT True.

(True AND True AND True) should be obvious that this is True

Not (True AND True AND True) The expression evaluates to True, thus it is False to say the expression does NOT result in True.

IIRC, when there are more than 2 expressions and OR is involved, it is more important how the operators are grouped. I'll venture to say that one operator that you seldom see is XOR, possibly because it's not broadly understood.
Hi Micron. The OP's requirement was originally stated as so:
CosmaL said:
I want to view the next 30 days records which must be or completed, or A approved or B approved.
Which could be read as, given a record is within the next 30 days, then show it if said record (1) needs to be completed, or (2) needs to be approved by/for A, or (3) needs to be approved by/for B.

So, if a record is within the next 30 days but needs all three (completed, approved A, and approved B), the question is, should it be shown/included in the result or not?

So, depending on the answer to that question, then the use of Not(C1 AND C2 AND C3) may or may not work some of the time.


Make sense?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 11:34
Joined
Feb 28, 2001
Messages
27,133
Even though there is such a thing as operator precedence, I always try to remember to use explicit parenthetical grouping rather than rely on (my very poor memory of) the rules of operator precedence.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 17:34
Joined
Sep 12, 2006
Messages
15,634
I was think more of changing this idea

WHERE (((tblMaintenanceActions.FinishedDate) Between Date() And Date()+30) AND ((tblMaintenanceActions.Completed)=False)) OR (((tblMaintenanceActions.AApproval)=False)) OR (((tblMaintenanceActions.BApproval)=False));

to this idea

WHERE (((tblMaintenanceActions.FinishedDate) Between Date() And Date()+30) AND NOT
(
((tblMaintenanceActions.Completed)=TRue)) AND (((tblMaintenanceActions.AApproval)=True)) AND
(((tblMaintenanceActions.BApproval)=TRue))
)

It seemed maybe easier as a general premise to me to resolve that the records you want are those within the date criteria and where some approval flag remains outstanding, but encompassing them all as NOT (true).

It's as broad as it's long in this case, I think, but with some complicated comparisons it's easier to reject the "true" items, then identify a "false" item.

(A similar approach is often taken to evaluating probabilities, where it's easier to evaluate the not true probability than the false probability)
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:34
Joined
Oct 29, 2018
Messages
21,449
I was think more of changing this idea

WHERE (((tblMaintenanceActions.FinishedDate) Between Date() And Date()+30) AND ((tblMaintenanceActions.Completed)=False)) OR (((tblMaintenanceActions.AApproval)=False)) OR (((tblMaintenanceActions.BApproval)=False));

to this idea

WHERE (((tblMaintenanceActions.FinishedDate) Between Date() And Date()+30) AND NOT
(
((tblMaintenanceActions.Completed)=TRue)) AND (((tblMaintenanceActions.AApproval)=True)) AND
(((tblMaintenanceActions.BApproval)=TRue))
)

It seemed maybe easier as a general premise to me to resolve that the records you want are those within the date criteria and where some approval flag remains outstanding, but encompassing them all as NOT (true).

It's as broad as it's long in this case, I think, but with some complicated comparisons it's easier to reject the "true" items, then identify a "false" item.

(A similar approach is often taken to evaluating probabilities, where it's easier to evaluate the not true probability than the false probability)
Hi Dave. I think I understood what you were trying to say. But as I was saying earlier, that logic version may work some of the times, but other times, it may not work, for this case, because 1 OR 2 OR 3 is not really the same as NOT(1 AND 2 AND 3).
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:34
Joined
Oct 29, 2018
Messages
21,449
Just to clarify what I was trying to say, I created the following table.



Running the following query:

Code:
SELECT test.*
FROM test
  WHERE (TestDate Between Date() And Date()+30)
   AND (A=True OR B=True OR C=True);
produced the following result:



And running the following query:

Code:
SELECT test.*
FROM test
 WHERE (TestDate Between Date() And Date()+30)
   AND Not (A=True AND B=True AND C=True);
produced the following result:




So, all I'm saying is, in this case, the two logic versions are not exactly the same (or won't always have the same result).

Hope that makes sense...
 

Attachments

  • table.png
    table.png
    11.6 KB · Views: 141
  • or.png
    or.png
    10.9 KB · Views: 144
  • not.png
    not.png
    11.6 KB · Views: 149

Users who are viewing this thread

Top Bottom