View Full Version : query for last record


jdev
11-16-2001, 05:15 PM
I have a list of 'products' and in another table, all the changes to name, price etc. The changes have an autoid# as well as a date field. How can I make a query which will display the product file info as well as the changes info, with only the most recent change info attached to each prioduct? I've tried using the ID# and the date field- every time I get an error message stating "Can't group on fields selected with *."
Does anyone have a suggestion? Thank you.

Pat Hartman
11-16-2001, 05:38 PM
This is a two part problem that is easily solved by a nested query. The inner query identifies the "max" record and the outer query retrieves the remaining columns. Assuming that your tables are related by ProductId and the changes table has a unique primary key that is an autonumber, the following should work.

query1:
Select ProductId, Max([autoid#]) As MaxAutoId
From YourTable
Group By ProductId;

query2:
Select q.ProductId, q.MaxAutoId, t.ProductName, t.Cost
From query1 As q Inner join YourTable on q.ProductId = t.ProductId and q.MaxAutoId = t.[autoid#];

jdev
11-17-2001, 07:25 AM
Thanks! I should have thought of that.