从表中依次取出1-300行,301-600行这样数据存入新表中,sql语句怎么写?

君立 2007-12-10 02:59:24
请教:从表中依次取出1-300行,301-600行这样数据存入新表中,sql语句怎么写?
select * from (select top 300 * from (select top ( 1 * 300 ) * from [new] order by [Col001])
order by [Col001] desc) order by [Col001]
但是发现上面语句top 在sql server 2005 中可以使用, 在2000版本中报错!
请问还有什么好语句实现以上功能
...全文
141 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
烟云1 2007-12-14
  • 打赏
  • 举报
回复
写一个递归的函数可以处理。具体可以查一下网上SQL的递归函数方法吧
中国风 2007-12-12
  • 打赏
  • 举报
回复


select
*
from
(select
top 300 *
from
(select top 600 * from T order by [Col001] desc ) T1
order by [Col001] asc)T2
order by [Col001] desc--加多一次排序
yuyuelong1975 2007-12-10
  • 打赏
  • 举报
回复
如果是sql2005,可以用row_number()来写:
WITH temp AS
(SELECT *,ROW_NUMBER() OVER (order by 排序名称)as RowNumber
FROM 表名 )
SELECT *
FROM temp
WHERE RowNumber between 301 and 600
要继续往下排,改变301和600就可以了
中国风 2007-12-10
  • 打赏
  • 举报
回复
--有唯一列时,用唯一列没有时用binary_checksum校验值所有列
--取301-600,按顺序小到大排列
select *
from
(select top 300 * from T where binary_checksum(*) not in(select top 300 binary_check(*) from T order by [Col001] desc ) order by [Col001] desc) TT
order by [Col001]
-狙击手- 2007-12-10
  • 打赏
  • 举报
回复
create table new(id int,col001 char(20))
go

select * from
(select top 300 * from (select top 300 * from [new] order by [Col001]) aaa
order by [Col001] desc) bbb order by [Col001]


drop table new
kk19840210 2007-12-10
  • 打赏
  • 举报
回复
select top 300 * from [new] where [Col001] not in (select top 300 [Col001] from [new] order   by   [Col001]) order   by   [Col001]
kk19840210 2007-12-10
  • 打赏
  • 举报
回复

select top 300 * from [new] where [Col001] not in (select top 300 [Col001] from [new] order by [Col001])) order by [Col001])

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧