select min(id) from ( select rn=row_number() over(order by getdate()),* from tb ) t where rn between 1 and 100 ?
我的意思是用top 100 id取得前100条记录,怎么在这基础上求这100个id中的最小值?
/* 例如数据表: id col 2 a 4 b 5 c 6 d 8 e 9 f 10 h 17 g */ create table #t (id int identity,rid int,col varchar(10)) insert into #t select id,col from tablenameselect min(rid) from #t where id>=100 导入到临时表,然后用临时表的自增列来取前100,这样就不用order by 了。
from
(
select rn=row_number() over(order by getdate()),* from tb
) t
where rn between 1 and 100
?
例如数据表:
id col
2 a
4 b
5 c
6 d
8 e
9 f
10 h
17 g
*/
create table #t (id int identity,rid int,col varchar(10))
insert into #t select id,col from tablenameselect min(rid) from #t where id>=100
导入到临时表,然后用临时表的自增列来取前100,这样就不用order by 了。
sql2005及以上的版本可以参考3楼,sql2000的需要使用子查询(如果表中没有自增列也可以使用自增列)和临时表来查
select top 100 min(number) from master..spt_values
select top 100 * from master..spt_values这个结果出现的最小值,并不在下面的top 100中。
版主和楼主一样蛋疼 这样求个最小id是没啥意义的,哪怕是随机,也应用个order by 随机函数