如何选择一列唯一的一组随机记录?

时间:2009-01-19 11:03:53

标签: sql random

我今天一直在努力解决这个SQL查询要求,我想知道是否有人可以帮助我。

我有一张体育问题表。其中一个栏目是与该问题相关的团队。我的要求是返回一组随机问题,其中团队是唯一的。

所以我们假设我们有下表并想要5个问题:

Question        Answer        Team
-----------------------------------
question 1      answer 1      team A
question 2      answer 2      team B
question 3      answer 3      team B
question 4      answer 3      team D
question 5      answer 3      team A
question 6      answer 3      team C
question 7      answer 3      team F
question 8      answer 3      team C
question 9      answer 3      team G
question 10     answer 3      team D

有效结果将返回:

question 1      answer 1      team A
question 2      answer 2      team B
question 4      answer 3      team D
question 6      answer 3      team C
question 7      answer 3      team F

我觉得应该可以通过巧妙地使用Distinct和Take来完成这个作为一个干净的SQL语句,但我还没有把它弄好。

到目前为止,最佳解决方案来自Mladen Prajdic。我稍微更新了它以改善它的随机性:

SELECT TOP 10 * 
FROM    (SELECT ROW_NUMBER() OVER(PARTITION BY Team ORDER BY Team, NEWID()) AS RN, *
    FROM Question
    ) teams
WHERE   RN = 2
ORDER BY NEWID()

3 个答案:

答案 0 :(得分:2)

对于sql 2005,你可以这样做:

select top 5 * 
from    (
            select ROW_NUMBER() over(partition by team order by team) as RN, *
            from @t 
        ) t
where RN = 1
order by NEWID()

答案 1 :(得分:1)

这应该在oracle中做你需要的;对于不同的数据库,显然你需要使用它们的随机数源。可能有更好的方法;希望别人能指出我们:p

select question, answer, team
from
(
select question, answer, team, r
from
(
select 
    question, 
    answer, 
    team,
    rank() over (partition by team order by dbms_random.value) r 
from questions
)
where r = 1
order by dbms_random.value
) where rownum<=5;

测试代码:

create table questions(question varchar2(16), answer varchar2(16), team varchar2(16));

insert into questions(question, answer, team)
values ('question 1',      'answer 1',      'team A');

insert into questions(question, answer, team)
values ('question 2',      'answer 2',      'team B');

insert into questions(question, answer, team)
values ('question 3',      'answer 3',      'team B');

insert into questions(question, answer, team)
values ('question 4',      'answer 3',      'team D');

insert into questions(question, answer, team)
values ('question 5',      'answer 3',      'team A');

insert into questions(question, answer, team)
values ('question 6',      'answer 3',      'team C');

insert into questions(question, answer, team)
values ('question 7',      'answer 3',      'team F');

insert into questions(question, answer, team)
values ('question 8',      'answer 3',      'team C');

insert into questions(question, answer, team)
values ('question 9',      'answer 3',      'team G');

insert into questions(question, answer, team)
values ('question 10',    'answer 3',      'team D');

commit;

答案 2 :(得分:0)

在PostgreSQL(其中有不同之处)中,我可能会这样做:

select distinct on (Team) Question, Answer, Team from test order by Team, random() limit 5;

刚试过它。似乎工作。