Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear ALL,

I have table with values in sql like this

SID   ID       Name      version
1     1         A          1.0
2     1         B          1.1
3     1         C          1.2
4     2         D          1.0

Here I need to select Max values of version with ID and Name.

I need result based on above table

SID   ID     Name        version
3      1       C          1.2
4      2       D          1.0

How to select this?.

Thanks in Advance..!
Posted
Updated 25-Sep-12 0:51am
v3

Hi,
I acheived this output by following query

SQL
with cte(ID,version)
as
(
    select distinct ID,max(version) from Table_Name
    group by ID
)
select  SID,t.ID,Name,t.version from Table_Name t
join cte c on t.version = c.version and t.ID = c.ID
 
Share this answer
 
Comments
Maciej Los 25-Sep-12 16:53pm    
CTE - the highest level of programming in T-SQL language ;)
+5!
Try this:
SQL
SELECT t1.*
FROM (SELECT *
        FROM YourTable) AS t1
    RIGHT JOIN (SELECT [ID],  MAX([Version]) AS [Version]
    FROM YourTable
    GROUP BY [ID]) AS t2
    ON t1.[ID] = t2.[ID] AND t1.[Version] = t2.[Version]
 
Share this answer
 
v2
hiii ,

here is the query

select SID,ID,name,version from table_1 t where version=(select MAX(version) from table_1 where ID=t.ID ) order by SID
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900