Question

I'm basically trying to produce a report that displays a filtered list of all of our invoices for the current week. (This report will be done on wednesdays, so all of the invoices since the last wednesday)

This is the code that I have for my WHERE statement:

WHERE e.TotalCount = 1 OR e.LastThreeMonths = 1
AND OINV.[TaxDate] >= (GETDATE() - 7)

The first part (e.TotalCount....) is all fine and fits in, but even with the second filter in, the query still returns all of the results regardless of date.

Am I being a complete idiot or should this work as expected to..?

Was it helpful?

Solution

Add parenthesis to separate the 2nd condition from the 3rd:

WHERE (e.TotalCount = 1 OR e.LastThreeMonths = 1)
     AND OINV.[TaxDate] >= (GETDATE() - 7)

AND takes precedence over OR, so your criteria was not applying the date requirement to all lines, just those with e.LastThreeMonths = 1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top