|
Question : How o get most recent record from history?
|
|
OK, another newbie question here. Picture a table with three fields:
EmpNum EffDate Rate 1234 12/1/04 10.00 1234 4/25/05 15.00 1234 6/15/05 18.00 1234 8/17/05 21.50 1234 11/20/03 5.50
I want to get the current rate for an employee. I set up a query with totals. the fields in the query are:
EmpNum EffDate Rate
Total: Group By Max Last Sort Asc Asc
If you want to see the SQL statement Access created it's: SELECT Rates.EmpNum, Max(Rates.EffDate) AS MaxOfEffDate, Last(Rates.Rate) AS LastOfRate FROM Rates GROUP BY Rates.EmpNum ORDER BY Rates.EmpNum, Max(Rates.EffDate);
As expected, it returns 8/17/2005 for the EffDate, but returns 5.5 for the rate, not the 21.50 that corresponds to the most current record. I've also tried the other options for Total, but nothing works. How do I get the rate field to come from the most recent record?
I'm sure this is a no-brainer for you experienced Access users, but I'm stumped. In addition to an answer, I'm also open for suggestions as to how many points it should be worth.
Thanks, Dino
|
|
Answer : How o get most recent record from history?
|
|
I can't believe I did that - and that I just spent several minutes wondering why. "The subquery is sound" I'm telling myself. There's nothing wrong with it. No. Of course there isn't. Of course there's everything wrong with where I have it.
SELECT EmpNum, Rate, EffDate FROM Rates WHERE EffDate = (SELECT MAX(EffDate) FROM Rates AS R WHERE R.EmpNum = Rates.EmpNum)
Better!
|
|
|
|