6.筛选出客户第一次下的订单。
思路。利用rows=1来查询客户第一次下的订单记录。
with tabs as (
select ROW_NUMBER() over(partition by customerID order by insDT)
as rows,* from OP_Order
)
select * from tabs where rows = 1
select * from OP_Order
7.rows_number()可用于分页
思路:先把所有的产品筛选出来,然后对这些产品进行编号。然后在where子句中进行过滤。
--分页存储过程
create proc usp_GetMyPhotos
@pageIndex int, --当前页码
@pageSize int, --每页多少条
@pageCount int output --计算 总共多少页
as
declare @count int --总共多少条
select @count =COUNT(*) from Photos
set @pageCount = CEILING( @count*1.0/@pageSize)
select * from
(select *,ROW_NUMBER() over(order by pid desc) as num
from Photos) as t
where num between @pageSize*(@pageIndex-1) + 1 and @pageSize*@pageIndex
8.在使用over等函数时,over里头的分组及排序的执行晚于“where,group by,order by”的执行
select
ROW_NUMBER() over(partition by customerID order by insDT) as rows,
customerID,totalPrice, DID from OP_Order where insDT>'2011-07-22'
以上代码是先执行where子句,执行完后,再给每一条记录进行编号。