8

我正在处理在线问题申请。

我正在从数据库中获取记录。

我在10组中有SQL database1000问题。我的意思是每组包含 100 个问题。我如何从每组中随机抽取 20 个问题?我的意思是如何从每组中选择2 个(根据要求)随机问题?

4

3 回答 3

3

尝试:

SELECT TOP 20 * FROM [YourTable] ORDER By NEWID()

更多关于NEWID()

于 2012-12-19T06:13:46.373 回答
2

如果您需要从每个组中获得 20 个随机问题,这里是一个SQLFiddle 示例SetNum这是一个集合ID

select * from 
(
select t.*, 
ROW_NUMBER() 
over (partition by setNum order by NewId()) rNum from t
) t2 where rNum<=20
于 2012-12-19T06:28:34.160 回答
1

如果您想2从每个 SET 中获得随机问题,请尝试此操作...

SELECT TOP 2 * FROM (SELECT * FROM [YourTable] WHERE SET_ID = 1) ORDER By NEWID()
UNION
SELECT TOP 2 * FROM (SELECT * FROM [YourTable] WHERE SET_ID = 2) ORDER By NEWID()
UNION
.
.
.
.
UNION
SELECT TOP 2 * FROM (SELECT * FROM [YourTable] WHERE SET_ID = 10) ORDER By NEWID()
于 2012-12-19T06:28:11.253 回答