Created
August 14, 2017 12:30
-
-
Save allisontharp/3e9b4c25e8e278ace7e3a12a93a4640f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT currentRank.rank, currentRank.bggid, IFNULL(g.name, currentRank.bggid) AS gameName, historicalRank.RowNumber AS StreakInDays | |
FROM | |
( | |
SELECT hg.bggid, hg.rank | |
FROM hotgame AS hg | |
WHERE DATE(hg.date) = CURDATE() | |
) AS currentRank | |
JOIN | |
( | |
SELECT | |
hg.bggid | |
, hg.rank | |
, DATE(hg.date) AS DateCol | |
,(@rn:= if((@bggid = bggid) AND (@rank = rank), @rn + 1, | |
if((@bggid:= bggid) AND (@rank:= rank), 1, 1) | |
) | |
) as RowNumber | |
FROM | |
hotgame AS hg | |
CROSS JOIN (SELECT @bggid := 0, @rank:=0, @rn:= 0) var | |
) AS historicalRank ON historicalRank.bggid = currentRank.bggid AND historicalRank.rank = currentRank.rank AND historicalRank.DateCol = curdate() | |
LEFT JOIN games AS g ON g.bggid = currentRank.bggid | |
GROUP BY currentRank.bggid, currentRank.rank, g.name | |
ORDER BY currentRank.rank | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment