Rx_
Nothing In Moderation
- Local time
- Today, 16:07
- Joined
- Oct 22, 2009
- Messages
- 2,803
Is there a TSQL Statement like TOP 1 that would return the 2nd value?
Considering avoiding cursors.
The Test returns the TOP 1 of a sorted recordset.
About 99.85% of the time, this satisfies the Scalar Function.
For the 0.15% of the time, the next record in that sorted recordset needs to be evaluated.
Example: formula with a If - Then statement could be used.
If this record is < rule > then return value - and done
Else if Recordset Count > 1 then look to see if 2nd record is <rule > return value - and done
Found this example, it would allow an Ascending for first part of IF then the DESC for the Else. Would this be worth coding or can someone think of another solution?
The 2nd line selects the top 2 rows and by using 'ORDER BY ROW_COUNT DESC", the 2nd row is arranged as being first, then it is selected using TOP 1
SELECT TOP 1 COLUMN1, COLUMN2 from (
SELECT TOP 2 COLUMN1, COLUMN2 FROM Table) ORDER BY ROW_NUMBER DESC
Considering avoiding cursors.
The Test returns the TOP 1 of a sorted recordset.
About 99.85% of the time, this satisfies the Scalar Function.
For the 0.15% of the time, the next record in that sorted recordset needs to be evaluated.
Example: formula with a If - Then statement could be used.
If this record is < rule > then return value - and done
Else if Recordset Count > 1 then look to see if 2nd record is <rule > return value - and done
Found this example, it would allow an Ascending for first part of IF then the DESC for the Else. Would this be worth coding or can someone think of another solution?
The 2nd line selects the top 2 rows and by using 'ORDER BY ROW_COUNT DESC", the 2nd row is arranged as being first, then it is selected using TOP 1
SELECT TOP 1 COLUMN1, COLUMN2 from (
SELECT TOP 2 COLUMN1, COLUMN2 FROM Table) ORDER BY ROW_NUMBER DESC