如何从sql中的每一组中选择20个随机问题?

时间:2012-12-19 06:00:26

标签: c# sql sql-server-2008

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

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

我在{strong> 10 设置中SQL database持有 1000 问题。我的意思是每组包含100个问题。如何从每组中获取 20个随机问题?我的意思是如何从每个集合中选择 2 (根据请求)随机问题?

3 个答案:

答案 0 :(得分:2)

尝试:

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

更多关于NEWID()

答案 1 :(得分:2)

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

select * from 
(
select t.*, 
ROW_NUMBER() 
over (partition by setNum order by NewId()) rNum from t
) t2 where rNum<=20

答案 2 :(得分:1)

如果您想从每个SET 2随机提问,请尝试此操作

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()