Retrieving Most Recent Records...

Bubbis Thedog

Well-known Member
Joined
Jul 29, 2004
Messages
967
Howdy, I'd like to retrieve records matching the following criteria:

1) Location = "Here"
2) Action = "Added"
3) Date_of_Action = MAX(Date_of_Maximum) for each name in the Full_Name field

Here's an example of the data:

Code:
Full_Name     Extension     Location     Action     Date_of_Action
Bob             1             Here       Added        3/1/2005
Jane            2             Here       Added        3/2/2005
Bob             1                        Removed      3/8/2005
Sandy           3             Else       Added        3/9/2005
Bob             4                        Added        3/10/2005

I'd like my query to return

Code:
Full_Name     Extension     Action     Date_of_Action
Jane            2           Added        3/2/2005
Bob             4           Added        3/10/2005

Does this require two queries?

THANKS FOR ANY HELP, FRIENDS! :biggrin:
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Vinny said:
Does this require two queries?
Not as far as I can see.

Just use a totals query with the specified criteria against Location and Action.

And set Date_of_Action to Max.
 
Upvote 0
norie, I can't quite get it. I've tried

Code:
SELECT Full_Name, Extension, Location, Action, Date_of_Action
FROM MAIN
WHERE (Location="Here" AND Action="Added"
   AND Date_of_Action = (SELECT Max(Date_of_Action)
                         FROM MAIN
                         WHERE Location="Here" AND Action="Added");

but still only get the most recent record out of all records. What am I doing wrong here?

Thanks again for your help, norie.
 
Upvote 0
Why are you using a subquery for the Max date?

I tried this with data the you posted .

SELECT MAIN.Full_Name, MAIN.Extension, MAIN.Action, Max(MAIN.Date_of_Action) AS MaxOfDate_of_Action
FROM MAIN
WHERE (((MAIN.Location)="Here"))
GROUP BY MAIN.Full_Name, MAIN.Extension, MAIN.Action
HAVING (((MAIN.Action)="Added"));

And it returned what you wanted.
 
Upvote 0
I was using a subquery because I was looking at it the wrong way. I suspected that HAVING was the key early on, but neglected to work with it. :oops:

Thanks so much, norie, for your help once again. I'm trying!
 
Upvote 0
Wouldn't

SELECT MAIN.Full_Name, Max(MAIN.Date_of_Action) AS MaxOfDate_of_Action
FROM MAIN
WHERE (((MAIN.Location)="Here") AND ((MAIN.Action)="Added"))
GROUP BY MAIN.Full_Name;

suffice?
 
Upvote 0
Aladin

That's right but you would need to change the default Group By to Where and it wouldn't actually display Location and Action.
 
Upvote 0
Aladin, thanks for your help. I tested the statement that you suggested. It works fine until I add MAIN.Extension to the SELECT clause. I get an error telling me that

'You tried to execute a query that does not include the specified expression 'Extension' as part of an aggregate function.'

I really need to see the extension that belongs to the employee (MAIN.Full_Name) on the most recent date. I can't seem to figure out the fix. Can you assist me further, please?


Norie, thanks for your help, and sorry to keep nagging about this... I can't group by Extension because that will return a Full_Name more than once --one for each extension. You see, this is a log of employees and what extensions they belong to. If they change extensions, I need to log 1) when they changed and 2) what their new extension is. So, I think the only grouping will be by Full_Name. Am I wrong here?

So, when I remove MAIN.Extension from the grouping, I get the above error as well. Any suggestions?

Thanks to both of you.
 
Upvote 0
Remove the Main.Extension from the SELECT and GROUP BY.
 
Upvote 0

Forum statistics

Threads
1,221,875
Messages
6,162,563
Members
451,775
Latest member
Aiden Jenner

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