如何按马克斯分组?
我在声明中遇到了一些麻烦, I have a table like this
Player Number | Sport | Date |
---|---|---|
1 | soccer | 4/1 |
4 | basketball | 4/2 |
2 | basketball | 4/3 |
3 | soccer | 4/1 |
1 | baseball | 4/2 |
4 | basketball | 4/3 |
5 | soccer | 4/1 |
3 | baseball | 4/2 |
5 | soccer | 4/2 |
2 | basketball | 4/3 |
1 | soccer | 4/4 |
2 | baseball | 4/5 |
3 | soccer | 4/4 |
4 | soccer | 4/5 |
5 | soccer | 4/3 |
5 | basketball | 4/5 |
I'm trying to write a query that will give me a table that will group together the players by their most played sport so the end result will look like this:
Sport | # of Players Who played the sport the most |
---|---|
Baseball | 2 |
Basketball | 1 |
Soccer | 2 |
I was able to write a statement that shows the每个球员都参加AA特定运动的次数,但是很难找到桌子,只能选择比赛数量最多的行,并安排每种运动将其分组。
Select distinct PLAYER_NUMBER, SPORT, Max(GAMES_PLAYED) as GAMES_PLAYED
FROM #temp
Group By SPORT, PLAYER_NUMER
order by GAMES_PLAYED desc
I'm having some trouble with a statement,
I have a table like this
Player Number | Sport | Date |
---|---|---|
1 | soccer | 4/1 |
4 | basketball | 4/2 |
2 | basketball | 4/3 |
3 | soccer | 4/1 |
1 | baseball | 4/2 |
4 | basketball | 4/3 |
5 | soccer | 4/1 |
3 | baseball | 4/2 |
5 | soccer | 4/2 |
2 | basketball | 4/3 |
1 | soccer | 4/4 |
2 | baseball | 4/5 |
3 | soccer | 4/4 |
4 | soccer | 4/5 |
5 | soccer | 4/3 |
5 | basketball | 4/5 |
I'm trying to write a query that will give me a table that will group together the players by their most played sport so the end result will look like this:
Sport | # of Players Who played the sport the most |
---|---|
Baseball | 2 |
Basketball | 1 |
Soccer | 2 |
I was able to write a statement that shows the number of times each player played a a specific sport but am having trouble getting the table to only select the rows with the highest number of plays and arranging it to be grouped by each sport.
Select distinct PLAYER_NUMBER, SPORT, Max(GAMES_PLAYED) as GAMES_PLAYED
FROM #temp
Group By SPORT, PLAYER_NUMER
order by GAMES_PLAYED desc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您想要的结果与您的示例数据不匹配?
无论如何,从查询开始获取每个玩家的比赛次数,然后使用
row_number
窗口函数来确定每个玩家最常玩的运动项目(请注意在以下情况下您想要做什么领带?)。然后在下一个查询中仅使用每个运动员和按运动分组最多的运动。返回: 参加
以下相关查询允许确认上面显示的结果:
其中显示每个运动员参加最多的运动,即每个运动员的第一行,以及请注意,棒球从来没有成为最受欢迎的运动!实际结果是使用每个
rn=1
计算的,因为这是每个玩家参与最多的运动。I don't think your desired results match your sample data?
Anyway start with a query to get the number of plays per player, and use the
row_number
window function to determine which sport was played the most for each player (note what do you want to do in the case of a tie?). Then in the next query only use the sport with the most plays per player and group by sport.Returns:
The following related query allows one to confirm the results shown above:
Which shows for each player which sport was played the most i.e. its the first row for each player, and note baseball never shows up as the most played sport! The actual results are calculated using every
rn=1
because that is the sport most played per player.理解您,
这
您 rel =“ nofollow noreferrer”> dbfiddle 在这里
If I understand you right, you might also get this by subqueries
this gets me
DBFiddle here