To anyone who can help. I am not an expert at T-SQL so I am coming here for some guidance. I have a table in SQL Server 2005 where each record has a revision number. What I need to be able to do is display all the records in the table with only the highest revision number. I have been working on this for a while and have tried using MAX() with sub queries. I also have tried the HAVING statement with the GROUP BY clause. I must not be using these correctly.
It is worth noting that I have many columns in this table but I stripped it down for this example. The GROUP BY was an issue because of the various other columns (TEXT, NVARCHAR) that I had to place in the clause. This caused my record counts to change.
Here is some sample data:
ID | RevisionNumber
0|0
0|1
0|2
1|0
1|1
1|2
1|3
1|4
Here is the expected result:
ID | RevisionNumber
0|2
1|4
My SQL Attempt:
SELECT
[ChangeRequests].[F0] AS [ID]
,MAX([ChangeRequests].[F8]) AS [RevisionNumber]
,[ChangeRequests].[F18] AS [ChangeNumber]
,CAST([ChangeRequests].[F19] AS NVARCHAR) AS [Synopsis]
,[ChangeRequests].[F30] AS [Responsibility]
,CAST([ChangeRequests].[F32] AS NVARCHAR) AS [Description]
,(CASE [ChangeRequests].[F42] WHEN 0 THEN NULL ELSE dbo.ConvertSTTimestamp([ChangeRequests].[F42]) END) AS [EnteredOn]
FROM [S37] AS [ChangeRequests]
GROUP BY
[ChangeRequests].[F0]
,[ChangeRequests].[F18]
,CAST([ChangeRequests].[F19] AS NVARCHAR)
,[ChangeRequests].[F30]
,CAST([ChangeRequests].[F32] AS NVARCHAR)
,[ChangeRequests].[F42]
This is the query that did not work.
Thanks so much in advance for any help!
1 Answer