Click here to Skip to main content
16,012,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, please help

Im trying to get the grade for each car at the latest date.

Sample table:

car-index	installed_date	firebar-grade	frame-grade
CAR5	           05-Jul-12	          B	A
CAR3	           31-Jan-12	          A	A
CAR1	           05-Apr-12	          C	C
CAR1	           24-May-12	          D	D
CAR3	           18-May-12	          C	C
CAR1	           08-Feb-12	          C	A
CAR1	           18-May-12	          C	B
CAR5	           24-May-12	          D	C
CAR3	           11-May-12	          C	B
CAR5	           22-Aug-12	          B	B
CAR5	           17-Sep-12	          A	B


desired results:


car-index	installed_date	firebar-grade	frame-grade
CAR1	            24-May-12	       D	D
CAR3	            18-May-12	       C	C
CAR5	            17-Sep-12	       A	B







I've tried this, but the results are incorrect.

SQL
select car_index, frame_grade, firebar_grade, max(installed_date) as recentdate from car 
group by car_index, frame_grade, firebar_grade;
Posted

SQL
select * from car c where c.installed_date = (select MAX(installed_date) from car cr where cr.[car-index] = c.[car-index])
 
Share this answer
 
Comments
nikki88 9-Feb-12 1:45am    
Thank you very much Christain! it works perfectly :)
Try this

SQL
SELECT C.car_index,MAX(C.firebar_grade),MAX(C.frame_grade) FROM Car C 
Group by C.car_index 


Hope this helps if yes then accept and vote the answer otherwise revert back with your queries
--Rahul D.
 
Share this answer
 
Comments
Christian Graus 8-Feb-12 10:03am    
I don't think that has any chance of working. It will grab the MAX value, not the one associated with the date.

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