如何按马克斯分组?

发布于 2025-01-18 17:58:41 字数 1781 浏览 0 评论 0原文

我在声明中遇到了一些麻烦, I have a table like this

Player NumberSportDate
1soccer4/1
4basketball4/2
2basketball4/3
3soccer4/1
1baseball4/2
4basketball4/3
5soccer4/1
3baseball4/2
5soccer4/2
2basketball4/3
1soccer4/4
2baseball4/5
3soccer4/4
4soccer4/5
5soccer4/3
5basketball4/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
Baseball2
Basketball1
Soccer2

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 NumberSportDate
1soccer4/1
4basketball4/2
2basketball4/3
3soccer4/1
1baseball4/2
4basketball4/3
5soccer4/1
3baseball4/2
5soccer4/2
2basketball4/3
1soccer4/4
2baseball4/5
3soccer4/4
4soccer4/5
5soccer4/3
5basketball4/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
Baseball2
Basketball1
Soccer2

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟织青萝梦 2025-01-25 17:58:41

我认为您想要的结果与您的示例数据不匹配?

无论如何,从查询开始获取每个玩家的比赛次数,然后使用 row_number 窗口函数来确定每个玩家最常玩的运动项目(请注意在以下情况下您想要做什么领带?)。然后在下一个查询中仅使用每个运动员和按运动分组最多的运动。

with cte as (
    select *
        -- For each player get the sport with the most plays
        , row_number() over (partition by Player order by count(*) desc) rn
    from #temp
    group by Player, Sport
)
select Sport
    , count(*) [# of Players Who played the sport the most]
from cte
where rn = 1
group by Sport
order by Sport;

返回: 参加

体育运动该运动最多的运动员的
编号篮球2
足球3

以下相关查询允许确认上面显示的结果:

with cte as (
    select *
        , count(*) num
        , row_number() over (partition by Player order by count(*) desc) rn
    from #temp
    group by Player, Sport
)
select Player, Sport, num
from cte
order by Player, num desc;

其中显示每个运动员参加最多的运动,即每个运动员的第一行,以及请注意,棒球从来没有成为最受欢迎的运动!实际结果是使用每个 rn=1 计算的,因为这是每个玩家参与最多的运动。

球员运动编号rn
1足球21
1棒球12
2篮球21
2棒球12
3足球21
3棒球12
4篮球21
4足球12
5足球31
5篮球12

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.

with cte as (
    select *
        -- For each player get the sport with the most plays
        , row_number() over (partition by Player order by count(*) desc) rn
    from #temp
    group by Player, Sport
)
select Sport
    , count(*) [# of Players Who played the sport the most]
from cte
where rn = 1
group by Sport
order by Sport;

Returns:

Sport# of Players Who played the sport the most
basketball2
soccer3

The following related query allows one to confirm the results shown above:

with cte as (
    select *
        , count(*) num
        , row_number() over (partition by Player order by count(*) desc) rn
    from #temp
    group by Player, Sport
)
select Player, Sport, num
from cte
order by Player, num desc;

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.

PlayerSportnumrn
1soccer21
1baseball12
2basketball21
2baseball12
3soccer21
3baseball12
4basketball21
4soccer12
5soccer31
5basketball12
只是在用心讲痛 2025-01-25 17:58:41

理解您,

select s.sport,
       count(distinct s.playdate) as times_played,
       (select max(t.players) from (
          select count(distinct s2.player) as players
          from   sports s2 
          where  s2.sport = s.sport 
          group by s2.sport, s2.playdate
          ) t
       ) as nbr_players
from   sports s
group by s.sport

一点
如果可能Subquies
正确获得通过

您 rel =“ nofollow noreferrer”> dbfiddle 在这里

If I understand you right, you might also get this by subqueries

select s.sport,
       count(distinct s.playdate) as times_played,
       (select max(t.players) from (
          select count(distinct s2.player) as players
          from   sports s2 
          where  s2.sport = s.sport 
          group by s2.sport, s2.playdate
          ) t
       ) as nbr_players
from   sports s
group by s.sport

this gets me

sporttimes_playednbr_players
baseball22
basketball32
soccer53

DBFiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文