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 |
|
jgurgen
Starting Member
4 Posts |
Posted - 2007-04-05 : 15:35:32
|
| i have a table with a loanid and a historycounter. The historycounter is a unique identifier for each transaction per loanid. the historycounter isnt in sequential order meaning, some of them could be in order of 2,3,4,5 or it could be 2,5,8,19.I need to do an update to each row to set balance=tranamt I was thinking of doing a select count to get the number of rows then do a loop to update each one, but because of the varying counters its not that simnple. So is there a way to select a certain row number to update it? Like using SELECT TOP 1 gives the first row, is there some kind of SELECT ROW 2 or something? Or is there a better way to do this? |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2007-04-05 : 17:15:12
|
In 2000 it is tricky, but in 2005 it is pretty simple, try thisWITH Ordered AS (SELECT *, ROW_NUMBER() OVER (ORDER BY historycounter) AS RowNumberFROM LoansWHERE loanid = 12345)SELECT * FROM OrderedWHERE RowNumber = 2 |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-04-06 : 04:13:03
|
| you could always doselect top 1 a.* from(select top 4 b.* from loans b order by historycounter desc) a |
 |
|
|
|
|
|