MySql中不使用日期的10周期移动平均线

发布于 2024-10-26 18:43:43 字数 1154 浏览 9 评论 0原文

我有一个守门员数据表,下面的片段

    year    gameid  player  sv% gamenum
2009    200165  John Smith  0.923   0165
2009    209754  John Smith  1.000   9754
2009    206938  John Smith  1.000   6938
2009    206155  John Smith  0.833   6155
2009    203021  John Smith  0.667   3021
2009    206472  John Smith  0.909   6472
2009    209524  John Smith  0.833   9524
2009    209351  John Smith  0.800   9351
2009    203056  John Smith  1.000   3056
2009    206761  John Smith  0.935   6761
2009    200466  John Smith  0.954   0466
2009    204171  John Smith  0.932   4171
2009    207876  John Smith  0.958   7876
2009    201581  John Smith  0.941   1581
2009    205286  John Smith  0.930   5286
2009    208991  John Smith  0.961   8991
2009    202696  John Smith  0.916   2696
2009    206401  John Smith  0.935   6401
2009    200106  John Smith  0.921   0106
2009    201381  John Smith  0.918   1381

我想获取每个守门员的 10 场比赛移动平均值,但我没有日期或比赛编号,例如他的第一场、第二场、第三场比赛等。游戏 ID 也是按照联赛级别的比赛顺序进行分配,因此 200106 场比赛可能是他本赛季的第一场比赛,200165 可能是他的第二场比赛,依此类推。

我的问题是:如何获得每年每个守门员分组的最大值(10 场比赛移动平均值)和最小值(10 场比赛移动平均值)?

另外,有没有办法使用 MySql 按守门员、年份对游戏 ID 进行排名?

I have a table of goalie data, snipet below

    year    gameid  player  sv% gamenum
2009    200165  John Smith  0.923   0165
2009    209754  John Smith  1.000   9754
2009    206938  John Smith  1.000   6938
2009    206155  John Smith  0.833   6155
2009    203021  John Smith  0.667   3021
2009    206472  John Smith  0.909   6472
2009    209524  John Smith  0.833   9524
2009    209351  John Smith  0.800   9351
2009    203056  John Smith  1.000   3056
2009    206761  John Smith  0.935   6761
2009    200466  John Smith  0.954   0466
2009    204171  John Smith  0.932   4171
2009    207876  John Smith  0.958   7876
2009    201581  John Smith  0.941   1581
2009    205286  John Smith  0.930   5286
2009    208991  John Smith  0.961   8991
2009    202696  John Smith  0.916   2696
2009    206401  John Smith  0.935   6401
2009    200106  John Smith  0.921   0106
2009    201381  John Smith  0.918   1381

I want to get the 10 game moving averages for each goalie, but I don't have dates or game numbers such as his first, second, third game, etc. The game ids are also assigned in the order they are played at the league level, so game 200106 could be his first game of season, and 200165 could be his 2nd, and so on.

My question is: How can I get the max(10 game moving average) and min(10 game moving average) grouped by each goalie for each year?

Also, is there a way to rank the game ids by goalie, year using MySql?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

撩心不撩汉 2024-11-02 18:43:43

10 场比赛移动平均值意味着如果您的比赛场数少于 10 场,则没有有意义的平均值(比赛次数不足)。如果您有 12 场比赛,则在 12 场比赛之间取平均值。

1-10 (avg)
2-11 (avg)
3-12 (avg)
max / min across the 3 averages

在 MySQL 中执行此操作的最有效方法是:

select .. (involving 13 @variables to rownumber and rotate the last
           10 values into the variables, keeping track of
           @player, @year, @rownumber)
order by player, year, gameid

这将仅传递数据一次,构建平均值。外部查询将简单地从此派生表中获取最小值/最大值。不过,我现在还不想充实这一点。

A 10 game moving average means that if you had less than 10 games, there is no meaningful average (not enough games). If you had 12 games, the average is taken between

1-10 (avg)
2-11 (avg)
3-12 (avg)
max / min across the 3 averages

The most efficient way to do this in MySQL would be to

select .. (involving 13 @variables to rownumber and rotate the last
           10 values into the variables, keeping track of
           @player, @year, @rownumber)
order by player, year, gameid

This will pass through the data only once, building the averages. An outer query will simply take min/max from this derived table. I'm not up for fleshing this out at the moment though.

挽袖吟 2024-11-02 18:43:43

这是一个想法(公平警告:未经测试)

SELECT max(mavg) FROM 
(SELECT (SELECT avg(avgfield),min(gamenum) as gn FROM YourTable g WHERE g.gamenum>t.gamenum LIMIT 10),t.gamenum 
       FROM
       YourTable t
) d

SELECT max(mavg) FROM 
(SELECT t.gamenum FROM
       YourTable t INNER JOIN 
       (SELECT avg(avgfield),min(gamenum) as gn FROM YourTable g WHERE g.gamenum>t.gamenum LIMIT 10) q  ON q.gn = t.gamenum
) d

This is one idea (fair warning:not tested)

SELECT max(mavg) FROM 
(SELECT (SELECT avg(avgfield),min(gamenum) as gn FROM YourTable g WHERE g.gamenum>t.gamenum LIMIT 10),t.gamenum 
       FROM
       YourTable t
) d

or

SELECT max(mavg) FROM 
(SELECT t.gamenum FROM
       YourTable t INNER JOIN 
       (SELECT avg(avgfield),min(gamenum) as gn FROM YourTable g WHERE g.gamenum>t.gamenum LIMIT 10) q  ON q.gn = t.gamenum
) d
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文