[英]MySQL select max count of a column value
I have table called Euro_Cup. 我有一个名为Euro_Cup的表。
[enter image description here][1] [在此处输入图片描述] [1]
Group Country Rank Jersey Position Age Selections Club Player
A Brazil 3 18 Midfielder 29 24 Internazionale Hernanes
A Cameroon 56 18 Midfielder 28 38 Antalyaspor Eyong Enoh
A Croatia 18 18 Forward 34 92 VfL Wolfsburg Ivica Olic
A Mexico 20 18 Defender 27 104 Bayer Leverkusen Andres Guardado
B Australia 62 18 Goalie 32 8 Adelaide United Eugene Galekovic
B Chile 14 18 Defender 28 65 Nottingham Forest Gonzalo Jara
B Spain 1 18 Defender 25 26 Barcelona Jordi Alba
B Netherlands 15 18 Midfielder 24 6 Norwich City Leroy Fer
I have to write a SQL query to group the player by their jersey number and then, for each jersey number group, return the most common position for that jersey number. 我必须编写一个SQL查询以按球员的球衣号码对球员进行分组,然后针对每个球衣号码组,返回该球衣号码的最常见位置。
Eg Jersey number 18 has 3 Midfilders, 8 defender and 2 Goalie then result should jersey number 18 , defender (bcos max player with jersey number number 18) 例如,球衣号码18的3名中场球员,8名后卫和2个守门员,则结果应为球衣号码18,后卫(球衣号码为18的bcos max球员)
I have tried 我努力了
select jersey,position,count(position) as cnt
from euro_cup2
group by jersey,position
having count(position) in
(select max(cnt) from (select jersey,position,count(position) as cnt
from euro_cup2
group by jersey,position)a)
But it does not work. 但这行不通。 Any help would be highly appreciated
任何帮助将不胜感激
I reviewed all your comments, this will give you what you search: 我查看了您的所有评论,这将为您提供搜索内容:
SELECT
euro_cup2.jersey,
a.position,
MAX(cnt) as count
FROM euro_cup2
LEFT JOIN
(SELECT
jersey,
position,
(COUNT(position)) AS cnt
FROM euro_cup2
GROUP BY jersey,position) AS a ON euro_cup2.jersey=a.jersey
GROUP BY jersey;
Here the result: 结果如下:
Regards 问候
Try this one, should work 试试这个,应该可以
SELECT Jersey,
Group_concat(Position separator ','),
Group_concat(CountOfPosition separator ',')
FROM (SELECT euro_cup.Jersey,
euro_cup.Position,
Count(euro_cup.Position) AS CountOfPosition
FROM euro_cup
GROUP BY euro_cup.jersey,
euro_cup.Position) a
WHERE CountOfPosition = (SELECT Max(CountOfPosition)
FROM (SELECT euro_cup.jersey,
euro_cup.Position,
Count(euro_cup.Position) AS CountOfPosition
FROM euro_cup
GROUP BY euro_cup.jersey,
euro_cup.Position) b
WHERE a.jersey = b.jersey)
GROUP BY Jersey
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.