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 |
|
cplusplus
Aged Yak Warrior
567 Posts |
Posted - 2011-06-23 : 14:27:15
|
| Is it possible if i run this query, i get 5 ID's, out of that i want to get the last but one SUID.select suid from tab_ccsnetsu where contractid=@contractid and cdrl=@CDRL and sutypeid=@SUTypeID order by suidSUID results are: it should give me 33, which is the last but one.27173345----Here is a sample select with all values:declare @TableSU table (SUID int)insert @TableSUselect 2 union allselect 7 union allselect 17 union allselect 33 union allselect 45select * from @TableSU----------------------thank you very much for the helpful info. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2011-06-23 : 14:43:41
|
Here are a couple ways:SELECT TOP 1 suidFROM( select TOP 2 suid from tab_ccsnetsu where contractid=@contractid and cdrl=@CDRL and sutypeid=@SUTypeID order by suid DESC) AS TORDER BY suid ASC -- OrSELECT suidFROM( select suid, ROW_NUMBER() OVER (ORDER BY suid DESC) AS RowNum from tab_ccsnetsu where contractid=@contractid and cdrl=@CDRL and sutypeid=@SUTypeID ) AS TWHERE RowNum = 2 |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-06-23 : 14:43:58
|
| Is it always the next to last suid as ordered?;with SUIDS as(select 2 as SUID UNIONselect 7 UNIONselect 17 UNIONselect 33 UNIONselect 45)select suidfrom(select suid ,row_number() OVER(order by suid desc) as rowfrom suids) awhere row > 1JimEveryday I learn something that somebody else already knew |
 |
|
|
cplusplus
Aged Yak Warrior
567 Posts |
Posted - 2011-06-23 : 15:07:11
|
| It worked, Excellent thank you very much GURUS. |
 |
|
|
|
|
|