1. 程式人生 > >按使用者分組按時間排序儲存過程

按使用者分組按時間排序儲存過程

create PROCEDURE [dbo].[fanhua_tasklistgroupbyuserid] 
(
@PageIndex int,
    @PageSize int
   )
AS
BEGIN
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
   declare @indextable table (id int identity(1,1),nid int)
   insert  @indextable (nid) select T.id from ( select row_number() over(partition by userid order by addtime desc) as rownum,* from fanhua_task ) as T where T.rownum=1 order by addtime desc
   select b.* from @indextable a, fanhua_task b where b.id =a.nid  and a.id between @PageLowerBound +1 and @PageUpperBound  order by b.addtime desc
end