根据条件(或类似条件)重设的逐行移动平均值

时间:2019-02-14 11:49:07

标签: mysql

我有一个数据集,其中包含每个灯具的团队排名。我想为每个灯具计算该等级的移动平均值。不同球员和固定装置的数量不是固定的,因此我需要能够计算每行的平均值,然后重置平均值以考虑适用于与最后一行不同的球员的排名值。 我在aws上运行mysql

我已经能够计算整个组合的移动平均值-只是不确定如何由玩家重置平均值

SELECT 
    date,
    playerName,
    fixtureRank,
    teamName,
    overallRank,
    vsTeamname,
    @overallRank:=@overallRank + overallRank totalRank,
    @RowNumber:=@RowNumber + 1 rowNumber
FROM
    evenmorePlayerData
    JOIN (SELECT @overallRank:=0) t
    JOIN (SELECT @RowNumber:=0) s

evenmorePlayerData的示例:


+---------------------+-------------+--------------+---------------+-------------+--------------------+
|        date         | playerName  | fixtureRtank |   teamName    | OverallRank |     vsTeamName     |
+---------------------+-------------+--------------+---------------+-------------+--------------------+
| 2018-09-05 00:00:00 | A Hodgson   |            1 | Mellor C      |          19 | Cheadle Hulme A    |
| 2018-09-17 00:00:00 | A Hodgson   |            1 | Mellor C      |          19 | Bramhall Village B |
| 2018-09-27 00:00:00 | A Hodgson   |            1 | Mellor C      |          19 | Manor C            |
| 2018-10-04 00:00:00 | A Hodgson   |            1 | Mellor C      |          19 | Disley D           |
| 2018-10-16 00:00:00 | A Hodgson   |            8 | Mellor B      |          17 | Mellor C           |
| 2018-11-01 00:00:00 | A Hodgson   |            9 | Mellor B      |          18 | Disley D           |
| 2018-11-06 00:00:00 | A Hodgson   |            1 | Mellor C      |          19 | Mellor B           |
| 2018-11-08 00:00:00 | A Hodgson   |            1 | Mellor C      |          19 | College Green D    |
| 2018-12-06 00:00:00 | A Hodgson   |            1 | Mellor C      |          19 | Poynton A          |
| 2018-12-13 00:00:00 | A Hodgson   |            9 | Mellor B      |          18 | Manor C            |
| 2019-01-17 00:00:00 | A Hodgson   |            1 | Mellor C      |          19 | Disley D           |
| 2019-01-24 00:00:00 | A Hodgson   |            1 | Mellor C      |          19 | Disley C           |
| 2018-09-04 00:00:00 | Adam Wilman |            1 | GHAP A        |           1 | Canute A           |
| 2018-09-25 00:00:00 | Adam Wilman |            1 | GHAP A        |           1 | Parrswood A        |
| 2018-09-27 00:00:00 | Adam Wilman |            1 | GHAP A        |           1 | Astrazeneca B      |
| 2018-11-06 00:00:00 | Adam Wilman |            1 | GHAP A        |           1 | Parrswood A        |
| 2019-01-22 00:00:00 | Adam Wilman |            1 | GHAP A        |           1 | Disley A           |
| 2019-01-25 00:00:00 | Adam Wilman |            1 | GHAP A        |           1 | Altrincham Central |
| 2019-02-05 00:00:00 | Adam Wilman |            1 | GHAP A        |           1 | Bramhall Village A |
| 2018-09-11 00:00:00 | Ade Hancox  |            2 | Astrazeneca A |           2 | Racketeers A       |
| 2018-09-17 00:00:00 | Ade Hancox  |            1 | Astrazeneca A |           1 | Racketeers A       |
| 2018-09-25 00:00:00 | Ade Hancox  |            1 | Astrazeneca A |           1 | Racketeers B       |
| 2018-09-30 00:00:00 | Ade Hancox  |            1 | Astrazeneca A |           1 | Syddal Park B      |
| 2018-10-09 00:00:00 | Ade Hancox  |            5 | Astrazeneca A |           5 | Carrington A       |
| 2018-10-16 00:00:00 | Ade Hancox  |            1 | Astrazeneca A |           1 | College Green A    |
| 2018-10-23 00:00:00 | Ade Hancox  |            5 | Astrazeneca A |           5 | Syddal Park A      |
+---------------------+-------------+--------------+---------------+-------------+--------------------+

当前查询结果(样本):


+---------------------+-------------+---------------+---------------+--------------+--------------------+-----------+------------+
|        date         |  playerName |  fixtureRtank |    teamName   |  overallRank |     vsTeamName     | totalRank |  rowNumber |
+---------------------+-------------+---------------+---------------+--------------+--------------------+-----------+------------+
| 2018-09-05 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Cheadle Hulme A    |        19 |          1 |
| 2018-09-17 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Bramhall Village B |        38 |          2 |
| 2018-09-27 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Manor C            |        57 |          3 |
| 2018-10-04 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Disley D           |        76 |          4 |
| 2018-10-16 00:00:00 | A Hodgson   |             8 | Mellor B      |           17 | Mellor C           |        93 |          5 |
| 2018-11-01 00:00:00 | A Hodgson   |             9 | Mellor B      |           18 | Disley D           |       111 |          6 |
| 2018-11-06 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Mellor B           |       130 |          7 |
| 2018-11-08 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | College Green D    |       149 |          8 |
| 2018-12-06 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Poynton A          |       168 |          9 |
| 2018-12-13 00:00:00 | A Hodgson   |             9 | Mellor B      |           18 | Manor C            |       186 |         10 |
| 2019-01-17 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Disley D           |       205 |         11 |
| 2019-01-24 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Disley C           |       224 |         12 |
| 2018-09-04 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Canute A           |       225 |         13 |
| 2018-09-25 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Parrswood A        |       226 |         14 |
| 2018-09-27 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Astrazeneca B      |       227 |         15 |
| 2018-11-06 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Parrswood A        |       228 |         16 |
| 2019-01-22 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Disley A           |       229 |         17 |
| 2019-01-25 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Altrincham Central |       230 |         18 |
| 2019-02-05 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Bramhall Village A |       231 |         19 |
| 2018-09-11 00:00:00 | Ade Hancox  |             2 | Astrazeneca A |            2 | Racketeers A       |       233 |         20 |
| 2018-09-17 00:00:00 | Ade Hancox  |             1 | Astrazeneca A |            1 | Racketeers A       |       234 |         21 |
| 2018-09-25 00:00:00 | Ade Hancox  |             1 | Astrazeneca A |            1 | Racketeers B       |       235 |         22 |
| 2018-09-30 00:00:00 | Ade Hancox  |             1 | Astrazeneca A |            1 | Syddal Park B      |       236 |         23 |
| 2018-10-09 00:00:00 | Ade Hancox  |             5 | Astrazeneca A |            5 | Carrington A       |       241 |         24 |
| 2018-10-16 00:00:00 | Ade Hancox  |             1 | Astrazeneca A |            1 | College Green A    |       242 |         25 |
+---------------------+-------------+---------------+---------------+--------------+--------------------+-----------+------------+


您可以看到行总数一直在增加,我真的不知道从哪里开始实现我的目标。

1 个答案:

答案 0 :(得分:0)

我可能已经弄清楚了...但是感觉很hacky,因此,如果有人有更好的解决方案,我仍然可以回答:

SELECT 
    date,
    playerName,
    fixtureRank,
    teamName,
    overallRank,
    vsTeamname,
    @currentName:= @currentName prevName,
    case when @currentName NOT LIKE playerName 
        THEN 
            @overallRank:= overallRank
        ELSE
            @overallRank:=@overallRank + overallRank
    END AS totalRank,
    case when @currentName NOT LIKE playerName 
        THEN 
            @RowNumber:= 1
        ELSE
            @RowNumber:=@RowNumber + 1
    END AS rowNumber,
    @overallRank / @RowNumber as averageRank,
    @currentName:= playerName currentName
FROM
evenmorePlayerData
        JOIN
    (SELECT @overallRank:=0) t
        JOIN
    (SELECT @RowNumber:=0) s
        JOIN
    (SELECT @currentName:="") r

似乎已经完成了。俩。


+---------------------+-------------+---------------+---------------+--------------+--------------------+-------------+-----------+------------+--------------------+--------------+
|        date         |  playerName |  fixtureRtank |    teamName   |  overallRank |     vsTeamName     |  prevName   | totalRank |  rowNumber |       avgRank      |  currentName |
+---------------------+-------------+---------------+---------------+--------------+--------------------+-------------+-----------+------------+--------------------+--------------+
| 2018-09-05 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Cheadle Hulme A    |             |        19 |          1 |                 19 | A Hodgson    |
| 2018-09-17 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Bramhall Village B | A Hodgson   |        38 |          2 |                 19 | A Hodgson    |
| 2018-09-27 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Manor C            | A Hodgson   |        57 |          3 |                 19 | A Hodgson    |
| 2018-10-04 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Disley D           | A Hodgson   |        76 |          4 |                 19 | A Hodgson    |
| 2018-10-16 00:00:00 | A Hodgson   |             8 | Mellor B      |           17 | Mellor C           | A Hodgson   |        93 |          5 |               18.6 | A Hodgson    |
| 2018-11-01 00:00:00 | A Hodgson   |             9 | Mellor B      |           18 | Disley D           | A Hodgson   |       111 |          6 |               18.5 | A Hodgson    |
| 2018-11-06 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Mellor B           | A Hodgson   |       130 |          7 | 18.571428571428573 | A Hodgson    |
| 2018-11-08 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | College Green D    | A Hodgson   |       149 |          8 |             18.625 | A Hodgson    |
| 2018-12-06 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Poynton A          | A Hodgson   |       168 |          9 | 18.666666666666668 | A Hodgson    |
| 2018-12-13 00:00:00 | A Hodgson   |             9 | Mellor B      |           18 | Manor C            | A Hodgson   |       186 |         10 |               18.6 | A Hodgson    |
| 2019-01-17 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Disley D           | A Hodgson   |       205 |         11 | 18.636363636363637 | A Hodgson    |
| 2019-01-24 00:00:00 | A Hodgson   |             1 | Mellor C      |           19 | Disley C           | A Hodgson   |       224 |         12 | 18.666666666666668 | A Hodgson    |
| 2018-09-04 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Canute A           | A Hodgson   |         1 |          1 |                  1 | Adam Wilman  |
| 2018-09-25 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Parrswood A        | Adam Wilman |         2 |          2 |                  1 | Adam Wilman  |
| 2018-09-27 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Astrazeneca B      | Adam Wilman |         3 |          3 |                  1 | Adam Wilman  |
| 2018-11-06 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Parrswood A        | Adam Wilman |         4 |          4 |                  1 | Adam Wilman  |
| 2019-01-22 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Disley A           | Adam Wilman |         5 |          5 |                  1 | Adam Wilman  |
| 2019-01-25 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Altrincham Central | Adam Wilman |         6 |          6 |                  1 | Adam Wilman  |
| 2019-02-05 00:00:00 | Adam Wilman |             1 | GHAP A        |            1 | Bramhall Village A | Adam Wilman |         7 |          7 |                  1 | Adam Wilman  |
| 2018-09-11 00:00:00 | Ade Hancox  |             2 | Astrazeneca A |            2 | Racketeers A       | Adam Wilman |         2 |          1 |                  2 | Ade Hancox   |
| 2018-09-17 00:00:00 | Ade Hancox  |             1 | Astrazeneca A |            1 | Racketeers A       | Ade Hancox  |         3 |          2 |                1.5 | Ade Hancox   |
| 2018-09-25 00:00:00 | Ade Hancox  |             1 | Astrazeneca A |            1 | Racketeers B       | Ade Hancox  |         4 |          3 | 1.3333333333333333 | Ade Hancox   |
| 2018-09-30 00:00:00 | Ade Hancox  |             1 | Astrazeneca A |            1 | Syddal Park B      | Ade Hancox  |         5 |          4 |               1.25 | Ade Hancox   |
| 2018-10-09 00:00:00 | Ade Hancox  |             5 | Astrazeneca A |            5 | Carrington A       | Ade Hancox  |        10 |          5 |                  2 | Ade Hancox   |
+---------------------+-------------+---------------+---------------+--------------+--------------------+-------------+-----------+------------+--------------------+--------------+