Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

the one before maxDate!

Status
Not open for further replies.

installer69

Programmer
Sep 18, 2004
35
GB
I have a query that finds the 'max' value of a now() field but how can I find the one before the max?
 
instead of using now() use date() -1.

That will give you todays date, minus 1 day.

I hope that is want you wanted if not let me know.
 
Sorry, I didn't give all the info.

The field that stores the now() data is a datestamp field for movements of stock. i use the maxDate lookup to find the last movement of each stock item. What I ned to do now is find the last but one movement of each item.

Craig
 
Something like this ?
SELECT A.itemfield,Max(A.nowfield) As maxBut1Date FROM theTable A INNER JOIN
(SELECT itemfield,Max(nowfield) As maxDate FROM theTable GROUP BY itemfield) B
ON A.itemfield = B.itemfield
WHERE A.nowfield < B.maxDate
GROUP BY A.itemfield
;

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
It works fine until I actually try to use it in the application. then I get an error about somethin like > and < are not supported and the query changes to:
Code:
SELECT A.serial, Max(A.actionDate) AS maxBut1Date
FROM master AS A INNER JOIN [SELECT serial,Max(actionDate) As maxDate FROM master GROUP BY serial] AS B ON A.serial=B.serial
WHERE A.actionDate<B.maxDate
GROUP BY A.serial;

Notice the 'AS' before A INNER JOIN and the () changed to [] around the second SELECT query
 
Access seems to change the parens () denoting a derived query into brakets[]. with a period - not sure if this is always the case. Try putting the period after the bracket.

SELECT A.serial, Max(A.actionDate) AS maxBut1Date
FROM master AS A INNER JOIN [SELECT serial,Max(actionDate) As maxDate FROM master GROUP BY serial]. AS B ON A.serial=B.serial
WHERE A.actionDate<B.maxDate
GROUP BY A.serial;
 
Thanks for the reply but I ended up curing the problem by inserting a space between the , and the Max. Compare:
Code:
SELECT A.itemfield, Max(A.nowfield) As maxBut1Date FROM theTable A INNER JOIN
(SELECT itemfield, Max(nowfield) As maxDate FROM theTable GROUP BY itemfield) B
ON A.itemfield = B.itemfield
WHERE A.nowfield < B.maxDate
GROUP BY A.itemfield
;
and
Code:
SELECT A.itemfield,Max(A.nowfield) As maxBut1Date FROM theTable A INNER JOIN
(SELECT itemfield,Max(nowfield) As maxDate FROM theTable GROUP BY itemfield) B
ON A.itemfield = B.itemfield
WHERE A.nowfield < B.maxDate
GROUP BY A.itemfield
;
Hope this helps someone else.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top