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 |
|
netsailer76
Starting Member
2 Posts |
Posted - 2010-07-10 : 13:47:43
|
| I have a table tblContract, In which there are three columns FlatID, FlatNumber ContractToDate the data goes as below.FlatId FlatNumber ContractToDate1 FLT1 01/01/20082 FLT2 01/01/20083 FLT3 01/01/20084 FLT1 01/01/20095 FLT2 01/01/20096 FLT3 01/01/2009 7 FLT1 01/01/20108 FLT2 01/01/20109 FLT3 01/01/2010 Now what i need is.. while i search for FLT1, i need FlatId which has the recent ContractToDate. Kindly note, FLT1 is there in ContractToDates, 2008,2009,2010 but with different FlatIds 1,4,9. The query should return 9 as result since it is the recent ContractToDate. Any help would be much appreciated. Thanks |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-07-10 : 18:58:16
|
| SELECT FlatIdFROM tblContract t1INNER JOIN (select max(ContractToDate) as ContractToDate from tblContract where FlatNumber = 'FLT1' ) t2ON t1.ContractToDate = t2.ContractToDateand t1.FlatNatNumber = 'FLT1'JimEveryday I learn something that somebody else already knew |
 |
|
|
netsailer76
Starting Member
2 Posts |
Posted - 2010-07-10 : 23:33:47
|
| works super thanks man. you are my STAR of the day |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-07-11 : 01:46:52
|
this should also do:select top 1 * from tblContract where FlatNumber = 'FLT1' order by ContractToDate desc |
 |
|
|
|
|
|