question

netsailer76 avatar image
netsailer76 asked

SQL Server 2008 Date

I have a table tblContract, In which there are three columns FlatID, FlatNumber ContractToDate the data goes as below. (FlatId, FlatNumber, ContractToDate) (1, FLT1, 01/01/2007) (2, FLT2, 01/01/2007) (3, FLT3, 01/01/2007) (4, FLT1, 01/01/2008) (5, FLT2, 01/01/2008) (6, FLT3, 01/01/2008) (7, FLT1, 01/01/2009) (8, FLT2, 01/01/2009) (9, FLT3, 01/01/2009) Now what i need is.. FlatId of the Recent Expiry (ContractToDate). Please observe Same flat number contractToDate is there in all three years. Please help with a Query. Regards

sql-server-2005datetime
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Squirrel 1 avatar image
Squirrel 1 answered
select *            
from            
(            
    select *, row_no = row_number() over (partition by FlatNumber order by ContractToDate desc)            
    from   tblContract            
) c            
where c.row_no = 1            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.