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!
Showing posts with label tinyint. Show all posts
Showing posts with label tinyint. Show all posts
Wednesday, March 7, 2012
Subscribe to:
Posts (Atom)