Home Page for the TeradataForum
 

Archives of the TeradataForum

Message Posted: Mon, 17 Jul 2006 @ 21:14:54 GMT


     
  <Prev Next>   <<First <Prev Next> Last>>  


Subj:   Re: How to get top N records
 
From:   Dieter Noeth

Bhakti Ulhas Bongale wrote:

  Is there any function, other than ranking, by which I can select the top N records of a result set?  


Why don't you want to use ranking?


  This query returns me say 2000 rows. I want only the top 50 rows. Is there any function similar to ROWNUM in Oracle that will give me the expected result?  


In V2R6.1 Microsoft's TOP syntax was implemented:

     SELECT TOP 50 * FROM ...

But this is proprietary, a better way is RANK/ROW_NUMBER:

     SELECT
     ...
     QUALIFY
        ROW_NUMBER() OVER (ORDER BY field1, field2, field3 DESC) <= 50

If you replace QUALIFY with a Derived Table & WHERE then it's Standard SQL and this is implemented in Oracle/DB2/MS SQL 2005, too.


Dieter



     
  <Prev Next>   <<First <Prev Next> Last>>  
 
 
 
 
 
 
 
 
  
  Top Home Privacy Feedback  
 
 
Copyright for the TeradataForum (TDATA-L), Manta BlueSky    
Copyright 2016 - All Rights Reserved    
Last Modified: 15 Jun 2023