码迷,mamicode.com
首页 > 数据库 > 详细

sql Server分组查询

时间:2015-09-19 00:46:03      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

CREATE TABLE test
(
 id int primary key not null identity,
 names varchar(20)
);

declare @i int;
set @i=1;
while(@i<1000)
begin
    insert into test values(t+cast(@i as varchar(8)));
    select @i=@i+1;
end
--方法1
select top 10 *
from test
where id not in
(
 --40是这么计算出来的:10*(5-1)
 select top 40 id from test order by id
)
order by id
--方法2
select top 10 *
from test
where id >
(
 select max(id)
 from 
  (
   select top 40 id from test order by id
  ) A
)
order by id;

--方法3
select top 10 *
from 
(
 select row_number() over(order by id) as rownumber,* from test
) A
where rownumber > 40
--方法4
select *
from 
(
 select row_number() over(order by id) as rownumber,* from test
) A
where rownumber >= 41 and rownumber <= 50

 

sql Server分组查询

标签:

原文地址:http://www.cnblogs.com/changshuo/p/4820637.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!