简体   繁体   English

SQL:从同一表和同一列中选择,只是计数不同

[英]SQL: select from same table and same column, just different counts

I have a table called names , and I want to select 2 names after being count(*) as uniq, and then another 2 names just from the entire sample pool. 我有一个名为names的表,我想在被count(*)作为uniq之后选择2个名字,然后仅从整个样本池中选择另外2个名字。

firstname
John
John
Jessica
Mary
Jessica
John
David
Walter

So the first 2 names would select from a pool of John , Jessica , and Mary etc giving them equal chances of being selected, while the second 2 names will select from the entire pool, so obvious bias will be given to John and Jessica with multiple rows. 因此,前两个名称将从JohnJessicaMary等的组合中选择,从而给他们相等的机会,而后两个名称将从整个组合中选择,因此, JohnJessica明显偏见将具有多个行。

I'm sure there's a way to do this but I just can't figure it out. 我敢肯定有办法做到这一点,但我只是想不通。 I want to do something like 我想做类似的事情

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

Is this possible? 这可能吗? Appreciate any pointers. 感谢任何指针。

I think you are close but you need some randomness for the sampling: 我认为您很亲密,但是您需要一些随机性进行采样:

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

As mentioned here you can use RAND or similar functions to achieve it depending on the database. 如此处所述您可以根据数据库使用RAND或类似函数来实现它。

MySQL: MySQL:

SELECT firstname 
FROM (SELECT firstname, COUNT(*) as count FROM names GROUP BY firstname) 
ORDER BY RAND()
LIMIT 2

PostgreSQL: PostgreSQL:

SELECT firstname 
FROM (SELECT firstname, COUNT(*) as count FROM names GROUP BY firstname)  
ORDER BY RANDOM()
LIMIT 2

Microsoft SQL Server: Microsoft SQL Server:

SELECT TOP 2 firstname 
FROM (SELECT firstname, COUNT(*) as count FROM names GROUP BY firstname) 
ORDER BY NEWID()

IBM DB2: 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

Oracle: 甲骨文:

SELECT firstname 
FROM(SELECT firstname, COUNT(*) as count FROM names GROUP BY firstname ORDER BY dbms_random.value )
WHERE rownum in (1,2)

Follow the similar approach for selecting from entire pool 遵循类似的方法从整个池中进行选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM