我有一个名为names
的表,我想在count(*)之后选择2个名称作为uniq,然后再从整个样本池中选择另外2个名称。
firstname
John
John
Jessica
Mary
Jessica
John
David
Walter
因此,前两个名称将从John
,Jessica
和Mary
等池中进行选择,从而为他们提供相同的选择机会,而后两个名称将从整个池,因此John
和Jessica
有多行显而易见的偏见。
我确信有办法做到这一点,但我无法理解。我想做像
这样的事情SELECT uniq.firstname
FROM (SELECT firstname, count(*) as count from names GROUP BY firstname) uniq
limit 2
AND
SELECT firstname
FROM (SELECT firstname from names) limit 2
这可能吗?感谢任何指针。
答案 0 :(得分:0)
我认为你很接近,但你需要一些随机性来进行抽样:
(SELECT uniq.firstname
FROM (SELECT firstname, count(*) as count from names GROUP BY firstname) uniq
ORDER BY rand()
limit 2
)
UNION ALL
(SELECT firstname
FROM from names
ORDER BY rand()
limit 2
)
答案 1 :(得分:0)
如上所述here,您可以使用RAND或类似功能来实现它,具体取决于数据库。
MySQL的:
SELECT firstname
FROM (SELECT firstname, COUNT(*) as count FROM names GROUP BY firstname)
ORDER BY RAND()
LIMIT 2
的PostgreSQL:
SELECT firstname
FROM (SELECT firstname, COUNT(*) as count FROM names GROUP BY firstname)
ORDER BY RANDOM()
LIMIT 2
Microsoft SQL Server:
SELECT TOP 2 firstname
FROM (SELECT firstname, COUNT(*) as count FROM names GROUP BY firstname)
ORDER BY NEWID()
IBM DB2:
SELECT firstname , RAND() as IDX
FROM (SELECT firstname, COUNT(*) as count FROM names GROUP BY firstname)
ORDER BY IDX FETCH FIRST 2 ROWS ONLY
甲骨文:
SELECT firstname
FROM(SELECT firstname, COUNT(*) as count FROM names GROUP BY firstname ORDER BY dbms_random.value )
WHERE rownum in (1,2)
按照类似的方法从整个池中进行选择