I have a table Test_GUID, with fields id as int PK IDENTITY, GUID as varchar(50), and Version as tinyint. I put data in it such as
id GUID Version
------
1 abc 1
2 abc 2
3 def 1
Then I tried to get the following result:
id GUID Version
------
1 abc 2
3 def 1
By using a query:
SELECT GUID, MAX(Version) AS MaxVersion, id
FROM dbo.Test_GUID
GROUP BY GUID
But then I get an error because id is not being aggregated.
This works
SELECT GUID, MAX(Version) AS MaxVersion
FROM dbo.Test_GUID
GROUP BY GUID
but then how do I get the id of each record?use a correlated subquery, it's the same as grouping
select id
, GUID
, Version as MaxVersion
from dbo.Test_GUID as foo
where Version
= ( select max(Version)
from dbo.Test_GUID
where GUID
= foo.GUID )|||Thanks, was able to incorporate this into other queries, works great!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment