toxic

备忘录

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

子查询得到逆序排列的前PageIndex*PageSize条记录,然后把这PageIndex*PageSize条记录按正序排列得到前PageSize条记录。OK了,这里的前PageSize条记录,实际上就是我们想要的记录。下面是一个具体的例子:
注:其中的PageSize(分页的大小)和PageIndex(分页索引,初始假设为0)均为变量,

方法1:
SELECT * FROM (SELECT TOP PageSize * FROM (SELECT TOP [PageIndex*PageSize] * FROM table ORDER BY id asc) as b ORDER BY id desc) as c ORDER BY id asc

 

方法2(推荐):

SELECT * from (SELECT row_number() over (order by id asc) as rownumber, * FROM tAvctoun WHERE t_world = 0  and tdate between '2012-11-1' and '2012-11-20') as t Where t.rownumber > 15 And t.rownumber <= 20

 

方法3(带group by):

SELECT * FROM (SELECT row_number() over (order by convert(varchar(10),dDate,120) asc) as rownumber,count(nUserNo) as persons,convert(varchar(10),dDate,120) as dDate FROM tUser GROUP BY convert(varchar(10),dDate,120) HAVING convert(varchar(10),dDate,120) BETWEEN '2011-11-24' and '2011-11-27') as t where t.rownumber > 0 and t.rownumber <=5

(group by 中 用函数查出来的列需要指定别名 不然会报错)

posted on 2012-11-19 17:50  toxic  阅读(7768)  评论(0编辑  收藏  举报