Max Date < A Certain Date

foxhound

Board Regular
Joined
Mar 21, 2003
Messages
182
I need a query or sql to pull records from a table where Max Date is less than a certain date. However, if the Max Date is greater than "the certain date" I don't want to exclude that record but instead pull the next Max Date for that record. Any suggestions?

Foxhound
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi foxhound,

I don't understand the problem 100%. Is "Max Date" a value entered by the user at a prompt either via a query, form or a report? Or is it a value stored in a table? What exactly is "that certain date" - is it a value stored in a table as part of a record? Plus I'm not sure what you mean by "but instead pull the next Max Date for that record".

From my limited understanding of the problem it sounds like you might want two different queries - one to pull the records where "that certain date" is less than the existing "Max Date" and the second to define a new "Max Date". Is this on the right track?

Andrew :)
 
Upvote 0
Thank you for responding. Sorry for the confusion –I will try to clarify.

I have a list of individuals with 10 fields (including: name, refnum, date, state, loc, note, etc…). First, I want to pull all records from the table where the date is less than another that I choose (when I choose 01/01/2005 it will show records for 2001, 2002, 2003, etc…). Then, if there are duplicate entries for the refnum field, I want to pull the record with the closest date to the one I chose before.

I appreciate any assistance you can offer.
 
Upvote 0
Hi foxhound,

[Original response edited due to lack of testing on my part]

You will need to do this in a two-step process if you are using queries.

The first query selects all data less than your date range (by entering your criteria into the criteria section of your query under "date") and the 2nd query uses the "max" function in a "total" query. You can't use the two functions in one query - it won't work (just tested it and hence I have edited this response).

For the 2nd query, add query 1, View -> Totals, select the fields you want, under "date" select "Max" - leave the rest as "Group By". The "date" criteria is set in the first query (e.g. <=[enter date] or <=#01/01/2005# etc.).

Please note that where some of the other variables change values (i.e. same refnum, different dates (ok), but say different notes) then the "group by" function will show the refnum twice given the notes changed (for example). So, you might only want to have the minimal number of fields you need on the 2nd query to identify the records you want. You can then have a 3rd query that is based on the 2nd query to pull through the rest of the data you need.

I hope that is not too confusing.

HTH, Andrew. :)
 
Upvote 0

Forum statistics

Threads
1,221,814
Messages
6,162,132
Members
451,743
Latest member
matt3388

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