Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a datatable with 3 columns CardNo,VehicleNO,ExpiryDate..I have to get those columns by comparing ExpiryDate with the present Date. Now CardNo and VehicleNo are Duplicates in each row and ExpiryDate changes(either Date or Time on the same date)What i want is I should be able to get the latest enterd ExpiryDate in those Duplicate Rows.I want access Query for that..
SQL
cardno vehicleno    expirydate
12	ap20aa4779	06/18/12 11:50:06
14	ap20aa4777	06/05/12 11:20:36
14	ap20aa4777	06/15/12 10:46:20
14	ap20aa4777	06/15/12 10:49:20
14	ap20aa4777	06/15/12 10:55:06
14	AP20AA4777	06/18/12 11:11:00
9523	AP123123	06/18/12 13:09:24

now the output should be
SQL
12	ap20aa4779  06/18/12 11:50:06
 14    AP20AA4777    06/18/12 11:11:00
9523 AP123123        06/18/12 13:09:24
Posted

This should works:
SQL
SELECT CardNo, VehicleNO, MAX(ExpiryDate) AS ExpiryDate
FROM YourTable
GROUP BY CardNo, VehicleNO
ORDER BY CardNo, VehicleNO
 
Share this answer
 
Comments
Sandeep Mewara 20-Jun-12 8:11am    
My 5! Looks good.
Maciej Los 20-Jun-12 8:14am    
Thank you, Sandeep ;)
Prasad_Kulkarni 20-Jun-12 8:18am    
So quick, +5!
Maciej Los 20-Jun-12 8:29am    
Thank you ;)
Sunny_Kumar_ 20-Jun-12 8:26am    
Good Work. my +5!
Try this:
SQL
SELECT CardNo, VehicleNo, expiryDate
     FROM YourTableName t1
     WHERE expiryDate = (SELECT MAX(expiryDate)
                     FROM YourTableName t2
                    WHERE t1.CardNo = t2.CardNo)
 
Share this answer
 
v2
Comments
Sandeep Mewara 20-Jun-12 8:12am    
My 5! This one too looks good.
Prasad_Kulkarni 20-Jun-12 8:17am    
Thank you Sandeep!
sandeep nagabhairava 20-Jun-12 8:27am    
thanks My 5!
Prasad_Kulkarni 20-Jun-12 23:33pm    
Glad it helps!
Thank you Sandeep!
Maciej Los 20-Jun-12 8:30am    
Good answer, 5!

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