簡體   English   中英

如何選擇一組唯一的隨機記錄?

[英]How to select a set number of random records where one column is unique?

今天,我一直在努力滿足這一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()

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

這應該在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;

在PostgreSQL(具有獨特的功能)中,我可能會做這樣的事情:

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

剛剛測試過。 似乎可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM