1

以下是使用虚拟概念的查询,但我希望它的优化形式不使用“虚拟表” 概念。

 with TEMP AS
          (

          Select RN from                                                            
          (                                                                         
          SELECT TCTITL,                                                            
          ROW_NUMBER() OVER ( ORDER BY TCTITL asc ) AS RN FROM PVTC ORDER BY        
          TCTITL asc                                                                
          )T 

          WHERE ((TCTITL >= 'AAPS  ' ))                                             
          ORDER BY TCTITL asc fetch first row only  )  

          , mytab as
          (

          Select * from
          (                                                           
         ( SELECT TCTITL, TCEDUR, TCSTZF, TCTTLE, TCLSHN, TCCEQ3,                  
        ROW_NUMBER() OVER ( ORDER BY TCTITL asc ) AS RN                           
         FROM PVTC ORDER BY TCTITL asc  )                                          
          ) Q

          ,  TEMP where Q.RN                                                     
          between (TEMP.RN + 0) and ( TEMP.RN + 3)                                  
          )
          select * from mytab  

注意:我有一个结果为 1 到 10 的表,但我希望两个指针之间的结果只说 3 到 10。这是此查询的目标。

更新:我有五条记录,我想根据 Row_Number 2 在 1 t0 5 范围内选择记录。(其中 1,2,3,4,5 显示 Row_number )

   col1    col2    col3 
    a   b   c   
    a1  b1  c1  
    -------------------------
    ---------------------------
    --------------------------
    a5  b5  c5  
4

1 回答 1

0

看来您只想获取前四行 where tctitl >= 'AAPS ',以及描述它们在表中排名的行号。如果是这样,我认为这可以解决问题:

select * from (
        select pvtc.*, row_number() over (order by tctitl) as rn from pvtc 
    )
    where (tctitl >= 'AAPS  ' )                                             
    order by tctitl
    fetch first four rows only;
于 2012-10-23T13:54:32.257 回答