SQL分页

方法一:
-----------------查询10条数据,并增加排序id--------------
select top 10 ROW_NUMBER() over (order by fileid desc )as num,* from [Samdps_Test].[dbo].[UT_DealerSource]
 ---------------- 在上面查询的10条数据中分成5组每组2条,查询第5组数据,也就是8-10
select top 2* from ( select top 10 ROW_NUMBER() over (order by id desc )as num,* from [Samdps_Test].[dbo].[UT_DealerSource]
order by id desc)a ---order by fileid desc 防止数据位置变化
where num between (2*4+1) and (2*4+2)

方法二:
--------------------------------查询出前10行数据----------------------
select NTILE(5) OVER(ORDER BY filecode DESC ) as num,* from (select top 10 * from[Samdps_Test].[dbo].[UT_DealerSource])a 
-----------------查询第5组数据-------------------

select * from (
select NTILE(5)over(order by id desc ) as num ,* from  (Select top 10* from [Samdps_Test].[dbo].[UT_DealerSource] order by id desc)a 
)b
where num=5


----------------------保险起见 --- 测试-------------
SELECT TOP 100 *
INTO #TEST
FROM [Samdps_Test].[dbo].[UT_DealerSource]
SELECT ROW_NUMBER() OVER (ORDER BY id ) AS [序号], NTILE(5) OVER(ORDER BY id) as z, *
INTO #TEST1
FROM #TEST
SELECT * FROM #TEST1
WHERE Z=3
DROP TABLE #TEST
DROP TABLE #TEST1













评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值