Oracle分析函数rownum的问题

5090阅读 0评论2013-06-18 快跑蚂蚁
分类:Oracle

rownum的数据是从1开始生成的
而且记住,rownum是在SQL运行期间生成的

rownum = 1   
For x in ( select * from t )   
Loop   
  if ( rownum = 1 )   
  then   
     output date   
     rownum = rownum+1   
  end if;   
End loop; 

 select * from t where rownum >=5 ; 
这条语句之所以永远不会输出结果是因为rownum必须先从1开始,先由1,然后2.3.....
上面这条语句也即是下面这一条,
rownum = 1   
For x in ( select * from t )   
Loop   
  if ( rownum >= 5 )   
  then   
     output data   
     rownum = rownum+1   
  end if;   
End loop;  
大家分析一下,条件肯定不为真,所以一直不会输出数据

分析函数的格式如下:
分析函数() over (partition by xxx order by yyy)
经过xxx分组和yyy的排序后,计算出分析函数的值
求这几个数的第二大值 9,7,5,3,1
 正解如下:
select x 
 from ( select x, row_number() over ( order by x desc ) r 
          from t 
        ) 
 where r = 2  

上一篇:要买一个1T的移动硬盘
下一篇:PostgreSQL学习手册(系统视图)