Where then Else? formula

jarett

Board Regular
Joined
Apr 12, 2021
Messages
175
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a "WHERE" statement in my last query

SQL:
WHERE (((CDate([DateCreated] & " " & [Order Time]))>DMax("dtmQueryLastRun","tblQueryLastRun")));

I need to add another parameter if the (CDate([DateCreated] & " " & [Order Time]) is < DMAX.... then WHERE (((CDate([NEWDATEFIELD])>DMax("dtmQueryLastRun","tblQueryLastRun")))

Can I add this to the same SQL or do I need to add another query?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You didn't mention what you want the ELSE to return... Anyway, the setup can be as simple as...

SQL:
WHERE IIF(CDate([DateCreated] & " " & [Order Time])>DMax("dtmQueryLastRun","tblQueryLastRun"),'CONDITION WHEN TRUE' , '... WHEN FALSE');
 
Upvote 0
I guess the ELSE would be WHERE (((CDate([NEWDATEFIELD])>DMax("dtmQueryLastRun","tblQueryLastRun"))) if it would even be considered an ELSE statement.
 
Upvote 0
You want the same criterion regardless of if the condition is met or not?
 
Upvote 0
maybe
SQL:
WHERE CDate([DateCreated] & " " & [Order Time])>DMax("dtmQueryLastRun","tblQueryLastRun") AND
 CDate([NEWDATEFIELD])>DMax("dtmQueryLastRun","tblQueryLastRun")
I probably have the ending parentheses in the wrong place for the first CDate. Not clear what's being asked for.
 
Upvote 0
You want the same criterion regardless of if the condition is met or not?
Hmm... trying to figure out how to explain it. So if the DateCreated+OrderTime is less than the tblQueryLastRun value I need to look to another field in the query "NEWFIELD". Then only allow records where the NEWFIELD is > tblQueryLastRun value.
 
Upvote 0
Confusing still but take a look at the IIF function. IIF(test, result if true, result if false)
I tried to construct a sample but got balled up in figuring out which parts were just the test and which were the criteria to be used for the results of the test from the different examples given.
 
Upvote 0
Hmm... trying to figure out how to explain it. So if the DateCreated+OrderTime is less than the tblQueryLastRun value I need to look to another field in the query "NEWFIELD". Then only allow records where the NEWFIELD is > tblQueryLastRun value.
I think I'm getting it. What happens if DateCreated+OrderTime = tblQueryLastRun value? You can do this one of two ways - first using the IIF() and second using grouped AND and OR.

SQL:
WHERE IIF(CDate([DateCreated] & " " & [Order Time])>DMax("dtmQueryLastRun","tblQueryLastRun"), CDate([DateCreated] & " " & [Order Time])>DMax("dtmQueryLastRun","tblQueryLastRun") ,  CDate([NEWDATEFIELD])<DMax("dtmQueryLastRun","tblQueryLastRun"));

SQL:
WHERE (CDate([DateCreated] & " " & [Order Time])>DMax("dtmQueryLastRun","tblQueryLastRun") AND CDate([NEWDATEFIELD])<DMax("dtmQueryLastRun","tblQueryLastRun")) OR (CDate([DateCreated] & " " & [Order Time])<DMax("dtmQueryLastRun","tblQueryLastRun")  AND CDate([NEWDATEFIELD])>DMax("dtmQueryLastRun","tblQueryLastRun"));

Depending on which of these you want to have when DateCreated+OrderTime = the tblQueryLastRun, you will change the > or < to >= or <=, respectively. And you will want to give careful thought to that and make sure you actually make those updates so you don't miss records you might be expecting to have returned.
 
Upvote 0
Solution
I think I'm getting it. What happens if DateCreated+OrderTime = tblQueryLastRun value? You can do this one of two ways - first using the IIF() and second using grouped AND and OR.

SQL:
WHERE IIF(CDate([DateCreated] & " " & [Order Time])>DMax("dtmQueryLastRun","tblQueryLastRun"), CDate([DateCreated] & " " & [Order Time])>DMax("dtmQueryLastRun","tblQueryLastRun") ,  CDate([NEWDATEFIELD])<DMax("dtmQueryLastRun","tblQueryLastRun"));

SQL:
WHERE (CDate([DateCreated] & " " & [Order Time])>DMax("dtmQueryLastRun","tblQueryLastRun") AND CDate([NEWDATEFIELD])<DMax("dtmQueryLastRun","tblQueryLastRun")) OR (CDate([DateCreated] & " " & [Order Time])<DMax("dtmQueryLastRun","tblQueryLastRun")  AND CDate([NEWDATEFIELD])>DMax("dtmQueryLastRun","tblQueryLastRun"));

Depending on which of these you want to have when DateCreated+OrderTime = the tblQueryLastRun, you will change the > or < to >= or <=, respectively. And you will want to give careful thought to that and make sure you actually make those updates so you don't miss records you might be expecting to have returned.
I don't think I could ever get the values to be equal, or should I say not impossible but unlikely, an order would have to be created the exact time the query was run down to the second. Thanks I will try those out.
 
Upvote 0
Been trying a couple different ways with no luck, the value in the Query last run table that the formula is referencing is 5/20/2021 9:38:23 AM, my Date Created+OrderTime value is 5/20/2021 9:21:40 AM, and the DateUpdate+Ordertime1 value is 5/20/2021 9:39:49 AM. So in theory the query should filter the record out since the "DateCreated" value is < 5/20/2021 9:38:23 AM "QueryLastRun" but then look to the next criteria which should allow the record to be included in the query results because the "DateUpdate" is > 5/20/2021 9:38:23 AM "QueryLastRun". My query result is not showing any records though. This is the whole SQL, nothing else has changed expect for the WHERE line, was getting correct results before testing these options. If I change the CDate([DateUpdated] & " " & [Order Time1]))>DMax("dtmQueryLastRun","tblQueryLastRun") to a < then the query shows all results except the record that I am trying to produce (this is correct if I am reading it correctly), so not sure why it doesn't produce the correct result when I change it back to >.

SQL:
SELECT po_detail2.PurchaseOrderNo, Left([UDF_UNIQUE_KEY],Len([UDF_UNIQUE_KEY])-1) AS INVENTORY_KEY, Trim([po_detail2!Qty Ordered]) AS Qty, Right([UDF_UNIQUE_KEY],1) AS SIZE_INDEX, CDate([DateCreated] & " " & [Order Time]) AS Expr1, CDate([DateUpdated] & " " & [Order Time1])
FROM po_detail2 INNER JOIN IM068_MXPUnivProdCode ON (po_detail2.LotSerialNo = IM068_MXPUnivProdCode.LotSerialNo) AND (po_detail2.ItemCode = IM068_MXPUnivProdCode.ItemCode)
WHERE IIF(CDate([DateCreated] & " " & [Order Time])>DMax("dtmQueryLastRun","tblQueryLastRun"), CDate([DateCreated] & " " & [Order Time])>DMax("dtmQueryLastRun","tblQueryLastRun"), CDate([DateUpdated] & " " & [Order Time1]))>DMax("dtmQueryLastRun","tblQueryLastRun")
ORDER BY po_detail2.PurchaseOrderNo DESC;
 
Upvote 0

Forum statistics

Threads
1,221,537
Messages
6,160,394
Members
451,645
Latest member
hglymph

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top