I'm using SQL Server. My current query looks like this:
SELECT
v.DocumentID, d.Filename, toolloc.Location AS ToolLocation,
temploc.Location AS TemplateLocation
FROM
SWPDM_EngVault.dbo.VariableValue AS v
LEFT OUTER JOIN
SWPDM_EngVault.dbo.Documents AS d ON v.DocumentID = d.DocumentID
LEFT OUTER JOIN
SWPDM_EngVault.dbo.DocumentsInProjects AS p ON d.DocumentID = p.DocumentID
LEFT OUTER JOIN
SWPDM_EngVault.dbo.Projects AS pa ON p.DocumentID = pa.ProjectID
LEFT OUTER JOIN
dbo.SL24ToolLocations AS toolloc ON v.DocumentID = toolloc.DocumentID
LEFT OUTER JOIN
dbo.SL24TemplateLocations AS temploc ON v.DocumentID = temploc.DocumentID
WHERE
(d.Deleted <> 1)
AND (d.Filename LIKE '%sldasm%')
AND (d.Filename LIKE 'FF0%')
AND (d.Filename LIKE '%tool%')
AND (d.Filename NOT LIKE '%flami%')
AND (d.Filename NOT LIKE '%filami%')
AND (d.Filename NOT LIKE '%delete%')
AND (v.VariableID = 107)
AND (v.ValueText = 'Tool')
AND (v.ConfigurationID = 2)
Let's say I get 1,000 results. My SWPDM_EngVault.dbo.VariableValue has two near duplicated records except that the column "RevisionNo" has different values in them. Let's say that those are 3 and 6. I need to get query results that have a count of 998 - without the rows that have a lower RevisionNo.
Example:
DocumentID RevisionNo
1 1
5 1
20 3
20 6
35 4
35 7
more rows ...
I only want to select the row in VariableValue that has the maximum value for RevisionNo. My current query is selecting all of them. I just want:
DocumentID RevisionNo
1 1
5 1
20 6
35 7
more rows ...
This query is a little past my ability. how would I select the row with the max value of VariableValue.RevisionNo while still doing all the joins?