如何将一个数据库中的记录分组。这个库中有200万个记录,每组分成6万个。如何写sql语句?

一大梨 2003-11-07 02:51:42
如何将一个数据库中的记录分组。这个库中有200万个记录,每组分成6万个。如何写sql语句?

我第一次用select top 60000 from table_name选了60000条,接下来该怎么办就不知道了,各位大虾请赐教!
...全文
78 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
一大梨 2003-11-07
  • 打赏
  • 举报
回复
同意twht(Hmily(心情不好)) ,用txlicenhe(马可)的方法查出来的是120000/,而不是60000行
twht 2003-11-07
  • 打赏
  • 举报
回复
txlicenhe(马可) 应该是不可以的,因为他查出来的是120000/180000行,而不是60000行,所以只能使用 wzh1215(四脚蛇) 的
zdleek 2003-11-07
  • 打赏
  • 举报
回复
在主键上建索引,根据索引排序
设主键名称为ID,
第一组
SELECT TOP 6000 * FROM TABLENAME ORDER BY ID

第二,。。。。组
SELECT TOP 6000 * FROM TABLENAME WHERE ID > @lID ORDER BY ID
其中@lID 为上一组中最后一条记录的id的值
pengdali 2003-11-07
  • 打赏
  • 举报
回复
如果你的主键是标识、或时间等可以标识记录先后顺序得话,可以这样优化:

select top 60000 * from table_name

--第二页:
select top 60000 * from table_name where 主键>@上一比最后一条记录的主键

--第三页:
select top 60000 * from table_name where 主键>@上一比最后一条记录的主键


...
pengdali 2003-11-07
  • 打赏
  • 举报
回复
select top 60000 * from table_name

--第二页:
select top 60000 * from table_name where 主键 not in (select top 60000 主键 from table_name)

--第三页:
select top 60000 * from table_name where 主键 not in (select top 120000 主键 from table_name)


...
txlicenhe 2003-11-07
  • 打赏
  • 举报
回复
假设主键为keyField
1: select top 60000 from table_name
2: select top 60000 from table_name where keyField in (Select top 120000 from table_name) and keyField not in (Select top 60000 keyField from table_name)
3: select top 60000 from table_name where keyField in (Select top 180000 from table_name) and keyField not in (Select top 12000 keyField from table_name)
...
Rotaxe 2003-11-07
  • 打赏
  • 举报
回复
select top 60000 * from (select top 12000 * from table_name order by 主键 asc) order by 主键 desc
wzh1215 2003-11-07
  • 打赏
  • 举报
回复
第二组:
select top 60000 from table_name where 主键 not in(select top 60000 主键 from table_name order by 主键) order by 主键
.....
txlicenhe 2003-11-07
  • 打赏
  • 举报
回复
假设主键为keyField
1: select top 60000 from table_name
2: Select top 120000 from table_name where keyField not in (Select top 60000 keyField from table_name)
3: Select top 180000 from table_name where keyField not in (Select top 120000 keyField from table_name)
...

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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