总的来说,问题是:我需要从一个表中选择一个值,而该表要参考另一个表中重复最多的值。
表具有以下结构: screenshot screenshot2
问题是要找到与之相关的运动员成果最多的国家。
首先,INNER JOIN表将结果与国家/地区联系起来
SELECT competition_id, country FROM result
INNER JOIN sportsman USING (sportsman_id);
然后,我计算每个国家出现的时间
SELECT country, COUNT(country) AS highest_participation
FROM (SELECT competition_id, country FROM result
INNER JOIN sportsman USING (sportsman_id))
GROUP BY country
;
并得到了这个screenshot3
现在感觉就像我离解决方案只有一步之遥)) 我想可以再使用SELECT FROM(SELECT ...)和MAX(),但我不能将其包装起来吗?
ps: 我这样做是通过将查询加倍来实现的,但是如果有数百万行,我觉得效率很低。SELECT country
FROM (SELECT country, COUNT(country) AS highest_participation
FROM (SELECT competition_id, country FROM result
INNER JOIN sportsman USING (sportsman_id)
) GROUP BY country
)
WHERE highest_participation = (SELECT MAX(highest_participation)
FROM (SELECT country, COUNT(country) AS highest_participation
FROM (SELECT competition_id, country FROM result
INNER JOIN sportsman USING (sportsman_id)
) GROUP BY country
))
我也是有观点的
CREATE VIEW temp AS
SELECT country as country_with_most_participations, COUNT(country) as country_participate_in_#_comp
FROM(
SELECT country, competition_id FROM result
INNER JOIN sportsman USING(sportsman_id)
)
GROUP BY country;
SELECT country_with_most_participations FROM temp
WHERE country_participate_in_#_comp = (SELECT MAX(country_participate_in_#_comp) FROM temp);
但不确定这是否是最简单的方法。
答案 0 :(得分:1)
您似乎过于复杂了。从现有的join
查询开始,您可以汇总,排序结果并仅保留第一行。
select s.country, count(*) cnt
from sportsman s
inner join result r using (sportsman_id)
group by s.country
order by cnt desc
fetch first 1 row with ties
请注意,如果有的话,这可以保持最高的领带。
答案 1 :(得分:1)
如果我正确理解这一点,则希望对每个比赛计数的国家/地区进行排名,并显示排名最高的一个或多个国家/地区。我建议您使用RANK
进行排名。
select country, competition_count
from
(
select
s.country,
count(*) as competition_count,
rank() over (order by count(*) desc) as rn
from sportsman s
inner join result r using (sportsman_id)
group by s.country
) ranked_by_count
where rn = 1
order by country;
如果结果行的顺序无关紧要,则可以将其缩短为:
select s.country, count(*) as competition_count
from sportsman s
inner join result r using (sportsman_id)
group by s.country
order by count(*) desc
fetch first rows with ties;
答案 2 :(得分:0)
SELECT country
FROM (SELECT country, COUNT(country) AS highest_participation
FROM (SELECT competition_id, country FROM result
INNER JOIN sportsman USING (sportsman_id)
) GROUP BY country
order by 2 desc
)
where rownum=1