请大家帮忙!!
10 个解决方案
#1
每前100W是靠什麼分組?
#2
怎么感觉需求有点BT
随机36条好不好
随机36条好不好
select top 36 * from tb order by newid()
#3
SELECT * FROM TB T WHERE ID IN (SELECT TOP 6 ID FROM TB WHERE NAME=T.NAME ORDER BY ID ASC)
#4
如果有连续的ID
select * from tb
where id between 1 and 6
or id between 1000001 and 1000006
or id between 2000001 and 2000006
or id between 3000001 and 3000006
or id between 4000001 and 4000006
or id between 5000001 and 5000006
or id between 6000001 and 6000006
#5
select *
from tb t
where id in(select top 6 id from tb where id/1000000=t.id/1000000)
#6
select DISTINCT *
from tb t
where id in(select top 6 id from tb where id/1000000=t.id/1000000)
#7
取n到m行
1.
select top m * from tablename where id not in (select top n id from tablename order by id asc/*|desc*/)
2.
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入到临时表
set rowcount n --只取n条结果
select * from 表变量 order by columnname desc
3.
select top n * from
(select top m * from tablename order by columnname) a
order by columnname desc
4.如果tablename里没有其他identity列,那么:
先生成一个序列,存储在一临时表中.
select identity(int) id0,* into #temp from tablename
取n到m条的语句为:
select * from #temp where id0 > =n and id0 <= m
如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
exec sp_dboption 你的DB名字,'select into/bulkcopy',true
5.如果表里有identity属性,那么简单:
select * from tablename where identity_col between n and m
6.SQL2005开始.可以使用row_number() over()生成行号
;with cte as
(
select id0=row_number() over(order by id),* from tablename
)
select * from cte where id0 between n to m
#8
如果没有连续的ID就构造一个
像海爷那样列举效率应该会快很多
select px=identity(int,1,1),* into # from tb order by id
select
id,col2,col3,... (除px外的其他字段)
from
#
where
px%1000000 between 1 and 6
像海爷那样列举效率应该会快很多
#9
/%的方法效率都太差,不想写
#10
学习
#1
每前100W是靠什麼分組?
#2
怎么感觉需求有点BT
随机36条好不好
随机36条好不好
select top 36 * from tb order by newid()
#3
SELECT * FROM TB T WHERE ID IN (SELECT TOP 6 ID FROM TB WHERE NAME=T.NAME ORDER BY ID ASC)
#4
如果有连续的ID
select * from tb
where id between 1 and 6
or id between 1000001 and 1000006
or id between 2000001 and 2000006
or id between 3000001 and 3000006
or id between 4000001 and 4000006
or id between 5000001 and 5000006
or id between 6000001 and 6000006
#5
select *
from tb t
where id in(select top 6 id from tb where id/1000000=t.id/1000000)
#6
select DISTINCT *
from tb t
where id in(select top 6 id from tb where id/1000000=t.id/1000000)
#7
取n到m行
1.
select top m * from tablename where id not in (select top n id from tablename order by id asc/*|desc*/)
2.
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入到临时表
set rowcount n --只取n条结果
select * from 表变量 order by columnname desc
3.
select top n * from
(select top m * from tablename order by columnname) a
order by columnname desc
4.如果tablename里没有其他identity列,那么:
先生成一个序列,存储在一临时表中.
select identity(int) id0,* into #temp from tablename
取n到m条的语句为:
select * from #temp where id0 > =n and id0 <= m
如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
exec sp_dboption 你的DB名字,'select into/bulkcopy',true
5.如果表里有identity属性,那么简单:
select * from tablename where identity_col between n and m
6.SQL2005开始.可以使用row_number() over()生成行号
;with cte as
(
select id0=row_number() over(order by id),* from tablename
)
select * from cte where id0 between n to m
#8
如果没有连续的ID就构造一个
像海爷那样列举效率应该会快很多
select px=identity(int,1,1),* into # from tb order by id
select
id,col2,col3,... (除px外的其他字段)
from
#
where
px%1000000 between 1 and 6
像海爷那样列举效率应该会快很多
#9
/%的方法效率都太差,不想写
#10
学习