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  | 
                             
                            
                                    | 
                                         vins 
                                        Starting Member 
                                         
                                        
                                        1 Post  | 
                                        
                                        
                                            
                                            
                                             Posted - 2011-12-01 : 03:31:43
                                            
  | 
                                             
                                            
                                            | How to retrive the nth highest values.without using functionstable name emp:id5060708070query:SELECT a.id FROM emp a WHERE 2 = (SELECT COUNT(distinct b.id) FROM emp b WHERE a.id <= b.id) ;results:id7070have analaysed subquery as below but did not get,kindly explain:b.id >= a.id50 >= 50 true50 >= 60 false50 >= 70 false50 >= 80 false50 >= 70 falsecount=1---------------b.id >= a.id60 >= 50 true60 >= 60 true60 >= 70 false60 >= 80 false60 >= 70 falsecount=2----------------b.id >= a.id70 >= 50 true70 >= 60 true70 >= 70 true70 >= 80 false70 >= 70 truecount=4-----------------b.id >= a.id80 >= 50 true80 >= 60 true80 >= 70 true80 >= 80 true80 >= 70 truecount=5-----------------b.id >= a.id70 >= 50 true70 >= 60 true70 >= 70 true70 >= 80 false70 >= 70 truecount=4----------------thanks in advance | 
                                             
                                         
                                     | 
                             
       
                            
                       
                          
                            
                                    | 
                                     Transact Charlie 
                                    Master Smack Fu Yak Hacker 
                                     
                                    
                                    3451 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2011-12-01 : 04:19:44
                                          
  | 
                                         
                                        
                                          There aren't really any nice ways  You can persist a ranking with the data but then you have to keep that up to date.Or you can do things like this:DECLARE @topN INT = 5SELECT TOP 1 topE.[ID]FROM    ( SELECT TOP (@topN) [ID] FROM emp ORDER BY [ID] ASC ) AS topEORDER BY    topE.[ID] DESC If you needed the N'th DISTINCT value (Though with a column named [ID]  it should be distinct anyway) Then you put a distinct into the derived table.Edit - multiple typos  Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     nigelrivett 
                                    Master Smack Fu Yak Hacker 
                                     
                                    
                                    3385 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2011-12-01 : 06:08:43
                                          
  | 
                                         
                                        
                                          | Maybeselect idfrom(select id, seq = rank() over (order by id desc) from tbl ) awhere seq = @rank==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 | 
                             
                         
                     | 
                 
             
         |