Need a query that does this...

  • Thread starter Thread starter 0to60
  • Start date Start date
0

0to60

Consider the following table:

ID dateField intFieldA intFieldB stringFieldA
stringFieldB

I want a query that will give me the row with MAX(dateField) grouped by ID.
If I say:

SELECT Max(dateField), ID
FROM table
GROUP BY ID;

Then that will give me each ID in the table and its maximum dateField. I
want the entire ROW that contains this maximum dateField, and I want it for
each ID. How do I write this query?
 
Do it in 2 seperate queries ( or in a union ).

Part 1 finds the max date and ID, part 2 links the date/ID back to the table
and pulls the row that matches those values.
 
SELECT table.ID, table.dateField, table.intFieldA,
table.intFieldB, table.stringFieldA, table.stringFieldB
FROM table
INNER JOIN
(SELECT Max(DateField), ID
FROM table
GROUP BY ID) AS Sub
ON table.ID = Sub.ID
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Query string getting corrupted 7
where in selection query? 2
DateTime Criteria?? 3
How to build a "reach around" query? 5
SQL Question 1
Determine Max Record 1
Access Query problem 1
Special characters 3

Back
Top