Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I've a table with following structure & data.

SQL
Column1	Column2	Column3	Column4
220	    2	    1506	17
220	    1	    1506	15
218	    1	    1497	5
217	    2	    1499	10
217	    1       1601	11


I Want to select all records of column1 with latest column2 values.

i.e. my result should look like this:

SQL
Column1 Column2 Column3 Column4
220     2       1506    17
218     1       1497    5
217     2       1499    10


Please suggest.

Thanks & Regards,
Abhishek Kumar
Posted
Comments
Mehdi Gholam 19-Sep-14 6:56am    
... and what is the criteria for being "latest" ?

Here you have to make one self join to get the maximum value
Try this
select  Max(a.Column1)as Column1 , Column2 from table a inner join table b on a.Column2=b.Column2 group by Column2


hope this helps you
thanks
 
Share this answer
 
 
Share this answer
 
Try this

SQL
SELECT T.*
FROM
(
   SELECT Column1, Max(Column2) Column2
   FROM   TABLE
   GROUP BY Column1
) A INNER JOIN TABLE T on A.Column1 = T.Column1 AND A.Column2 = T.Column2
 
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