开发者

Calculate the longest winning/losing streak for a user

I've already managed to make this to work but need help on improving the query and optimize it for fetching more parameters.

The query will calculate the longest streak for winning and losing a game and so far I've managed to make this to work by fetching win and lose as separate query's. But i would like to fetch them both at once. How can i make that to work by building further on this query (this one will calculate the longest win streak) :

SELECT MIN( c.ID ) - a.ID + 1 as Streak
    FROM games AS a
    LEFT JOIN games AS b ON a.ID = b.id + 1 AND b.P1Outcome= 'win'
    LEFT JOIN games AS c ON a.ID <= c.id AND c.P1Outcome= 'win'
    LEFT JOIN games AS d ON c.ID开发者_JAVA百科 = d.id - 1 AND d.P1Outcome= 'win'
    WHERE
      a.UsrID = x
      AND a.P1Outcome= 'win'
      AND b.ID IS NULL
      AND c.ID IS NOT NULL
      AND d.ID IS NULL
    GROUP BY a.ID
    ORDER BY Streak

ID = The primary key on each game

P1Outcome = The users outcome on the game

UsrID = The users ID

First of all can there be any improvements and if possible can it also fetch the lose streak at the same time? Thank you for your time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜