0

我有一个表“国家”(作为 SQLWorkbench 的演示提供的表),其属性为:“名称”和“独立年份”。

我想找到更多国家独立的年份,我对他们的名字不感兴趣。

此查询有效。

SELECT Nest.IndepYear, Nest.conta 
  FROM (Select IndepYear,count(IndepYear) AS conta 
   FROM country  GROUP BY IndepYear) Nest 
    WHERE Nest.conta = (Select max(conta) FROM (SELECT IndepYear,count(IndepYear) AS conta 
     FROM country GROUP BY IndepYear)Nest2)

输出

IndepYear   Conta
1960        18
1991        18

有没有更聪明、更易读或更有效的方法来做到这一点?

编辑:感谢到目前为止的答案,但我对获得最佳结果感兴趣。我的意思是只获取所有前 n 行和那些行。

4

2 回答 2

0

这远非完美,但可能会给您一些关于如何改进查询的想法:

select IndepYear, count(*) 
from country 
group by IndepYear 
having count(*) = (
  select count(*) from country group by IndepYear order by count(*) desc limit 1
)
于 2013-11-01T17:20:07.733 回答
0

如果我正确理解你的问题,下面的 SQL 应该可以解决问题:

SELECT IndepYear, count(IndepYear) conta
FROM Countries
GROUP BY IndepYear
ORDER By count(IndepYear) DESC

请参阅SQL Fiddle以获得完整的解决方案。

于 2013-11-01T17:29:49.340 回答