Player ranking by categories over months

懵懂的女人 提交于 2019-12-11 02:18:46

问题


Imagine the following Player table, with the fields:

PlayerId, Date, Kills and Gold

I need to get the player position by category (kills or gold) over months. This is the SELECT:

SET @rownumber := 0;

SELECT date, rank, kills FROM (
    SELECT pla.event_date, @rownumber := @rownumber + 1 AS rank, 
    pla.kills, pla.player_id
    FROM player AS pla
    INNER JOIN ...
    WHERE.pla.event_date >= '2017-09-01' AND pla.event_date <= '2017-12-31'
    ORDER BY pla.kills DESC
) AS result WHERE player_id = 651894

It works just fine when I filter it by one month. The problem is the @rownumber variable always increment +1 in the next month, which does not bring the right result.

I don't mean to make it a function to iterate over months from backend. How can I do it?


回答1:


You need another user variable to tell you when it's in a new month.

SET @rownumber := 0;
SET @month := 0;

SELECT pla.event_date, 
  @rownumber := IF(@month=MONTH(pla.event_date), @rownumber + 1, 1) AS rank, 
  @month := MONTH(pla.event_date) AS month,
  pla.kills, pla.player_id
FROM player AS pla
INNER JOIN ...
WHERE pla.event_date >= '2017-09-01' AND pla.event_date <= '2017-12-31'
ORDER BY MONTH(pla.event_date), pla.kills DESC


来源:https://stackoverflow.com/questions/48629538/player-ranking-by-categories-over-months

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!