I have a table which I am trying to write a query to get the latest record for each unique ID value. For simplicity of this thread, I am only going to include the two columns in this post which I think need to be included as part of the query. Let's say the two columns in the table look like the following:
SampleID NoOfRoutingChanges
S001 1
S001 2
S001 3
S002 1
S002 2
S003 1
S001 4
S002 3
With the above data, as you can see the SampleID will repeat but the NoOfRoutingChanges column will never repeat for the same SampleID. The query should return the rows with NoOfRoutingChanges equal to 4 for S001, 3 for S002 and 1 for S003. First, I tried to accomplish this using the following query but it every record:
Code: Select all
SELECT tblMain.SampleID, Max(tblSampleMain.SampleNoOfRoutingChanges) AS MaxOfSampleNoOfRoutingChanges
FROM tblSampleMain
GROUP BY tblSampleMain.SampleID
Code: Select all
SELECT tblMain.SampleID, Max(tblSampleMain.SampleNoOfRoutingChanges) AS MaxOfSampleNoOfRoutingChanges
FROM tblSampleMain
GROUP BY tblSampleMain.SampleID
HAVING (((Max(tblSampleMain.SampleNoOfRoutingChanges))>Nz((SELECT Max(tblSampleMain.VIRNoOfRoutingChanges) FROM tblSampleMain AS Q WHERE Q.SampleID = [tblSampleMain].[SampleID]),0)));