Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sara_23apr
Starting Member
8 Posts |
Posted - 2008-09-25 : 02:25:02
|
| Data in the data baseVeh No. Category Payment Date EffectiveDate Expiry Date Amt KA55 Permit 1-01-06 1-01-06 31-12-06 400KA55 Permit 24-01-07 25-01-07 31-12-07 500KA55 Permit 1-01-08 1-01-08 31-12-08 800KA100 Permit 24-01-07 25-01-07 31-12-07 500KA100 Permit 1-01-08 1-01-08 31-12-08 800KA55 Tax 24-01-07 25-01-07 31-12-07 300KA55 Tax 1-01-08 1-01-08 31-12-08 600KA55 Insurance 24-01-07 25-01-07 31-12-07 500KA55 Insurance 1-01-08 1-01-08 2-2-09 700Result neededVeh No. Category Payment Date Expiry Date AmountKA55 Permit 1-01-08 31-12-08 800KA100 Permit 1-01-08 31-12-08 800KA55 Tax 1-01-08 31-12-08 600KA55 Insurance 1-01-08 2-2-09 700Dear Friends I Need to show last record ( with highest “Expiry Date”) for each of the Vehicle number and category combination. Previously I just had to show the vehicle number , category and expiry date. so i had done a group by queryNow I do not know how to show the Payment date, vehicle number , category and expiry date, Amount in the same query Kindly help me RegardsSara |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-25 : 02:31:45
|
| [code]SELECT VehNo,Category,PaymentDate,ExpiryDate,AmountFROM(SELECT ROW_NUMBER() OVER(PARTITION BY VehNo,Category ORDER BY ExpiryDate DESC) AS Seq,*FROM YourTable)tWHERE t.Seq=1[/code]or [code]SELECT t.VehNo,t.Category,t.PaymentDate,t.ExpiryDate,t.AmountFROM YourTable tINNER JOIN (SELECT VehNo,Category,MAX(ExpiryDate) AS MaxExpiry FROM YourTable GROUP BY VehNo,Category)tmpON t.VehNo=tmp.VehNoAND t.Category=tmp.CategoryAND t.ExpiryDate=tmp.MaxExpiry[/code] |
 |
|
|
sara_23apr
Starting Member
8 Posts |
Posted - 2008-09-25 : 06:28:00
|
| Thank you for the replyWill try itSara |
 |
|
|
|
|
|
|
|