SQL的ROW_NUMBER函数

with tabs as  
(  
select ROW_NUMBER() over(partition by customerID  order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order  
 )  
select MAX(rows) as '下单次数',customerID from tabs group by customerID 

with tabs as  
 (  
select ROW_NUMBER() over(partition by customerID  order by insDT) as rows,customerID,totalPrice, DID from OP_Order  
)  
 select * from tabs  
where totalPrice in   
(  
select MIN(totalPrice)from tabs group by customerID  
 ) 

注意:在使用over等开窗函数时,over里头的分组及排序的执行晚于“wheregroup byorder by”的执行

select   
ROW_NUMBER() over(partition by customerID  order by insDT) as rows,  
customerID,totalPrice, DID  
from OP_Order where insDT>'2011-07-22' 

 

posted @ 2016-05-09 09:03  温故余学  阅读(199)  评论(0编辑  收藏  举报