I currently have two really complex queries that I use to bring in scores with ranks. The first is all the participants and their scores with stage ranks, the second is Grouped by each participant and calculates their overall rank (sum of stage ranks).
First query to get all participants for each stage with scores and ranks
SELECT * FROM (
SELECT alias, catabbr, sh_dq, raw, sdq, dnf,
@rownum := IF(@stage != stage_name, 1, @rownum + 1) AS rowNum,
@rank := IF(@prevVal != cTime, @rownum, @rank) AS rank,
@stage := stage_name as stage_name,
@prevVal := cTime AS cTime,
id, zeroTime
FROM
(SELECT @rownum:= 0) rn,
(SELECT @rank:= 1) av,
(SELECT @stage:= '') sv,
(SELECT @prevVal:= 0) pv,
(SELECT s.stage_name stage_name, sc.alias alias, sc.catAbbr catabbr, sc.sh_dq sh_dq, sc.time raw, sc.id id, sc.sdq sdq, sc.dnf dnf,
IF(sc.time = "0", 1, 0) AS zeroTime,
((ROUND(sc.time, 2) + (sc.miss * 5)) + ((sc.proc * 10) + (sc.saf * 10) + (sc.sog * 30)) - (sc.bthPoints)) cTime
FROM matches m
LEFT JOIN stages s ON s.match_id = m.id
LEFT JOIN scores sc ON sc.stage_id = s.id
WHERE m.matchuuid = 'E13A4C61-A2B8-48E2-BE1B-1FFB77CC5849'
GROUP BY sc.alias, s.stage_name
ORDER BY s.stage_name, zeroTime, cTime
) tv
) t
ORDER BY zeroTime, alias, stage_name;
Outputs:
+-----------+---------+-------+-----------------+-----+-----+--------+------+-----------------------+-------+----------+
| alias | catabbr | sh_dq | raw | sdq | dnf | rowNum | rank | stage_name | cTime | zeroTime |
+-----------+---------+-------+-----------------+-----+-----+--------+------+-----------------------+-------+----------+
| Back Bob | S | 0 | 52.9799995422 | 0 | 1 | 54 | 54 | Stage 1 | 92.98 | 0 |
+-----------+---------+-------+-----------------+-----+-----+--------+------+-----------------------+-------+----------+
| Back Bob | S | 0 | 43.9099998474 | 0 | 0 | 46 | 46 | Stage 2 | 48.91 | 0 |
+-----------+---------+-------+-----------------+-----+-----+--------+------+-----------------------+-------+----------+
| Ben Scal | ES | 0 | 26.9699993134 | 0 | 0 | 27 | 27 | Stage 1 | 31.97 | 0 |
+-----------+---------+-------+-----------------+-----+-----+--------+------+-----------------------+-------+----------+
| Ben Scal | ES | 0 | 32.8800010681 | 0 | 0 | 38 | 38 | Stage 2 | 42.88 | 0 |
+-----------+---------+-------+-----------------+-----+-----+--------+------+-----------------------+-------+----------+
Second query groups participants by name and sums their ranks for a final rank
SELECT alias, catabbr, SUM(cTime) fTime, SUM(rank) fRank, zeroTime, rank
FROM (
SELECT alias, catabbr, sh_dq, raw, sdq, dnf,
@rownum := IF(@stage != stage_name, 1, @rownum + 1) AS rowNum,
@rank := IF(@prevVal != cTime, @rownum, @rank) AS rank,
@stage := stage_name as stage_name,
@prevVal := cTime AS cTime,
id, zeroTime
FROM
(SELECT @rownum:= 0) rn,
(SELECT @rank:= 1) av,
(SELECT @stage:= '') sv,
(SELECT @prevVal:= 0) pv,
(SELECT s.stage_name stage_name, sc.alias alias, sc.catAbbr catabbr, sc.sh_dq sh_dq, sc.time raw, sc.id id, sc.sdq sdq, sc.dnf dnf,
IF(sc.time = "0", 1, 0) AS zeroTime,
((ROUND(sc.time, 2) + (sc.miss * 5)) + ((sc.proc * 10) + (sc.saf * 10) + (sc.sog * 30)) - (sc.bthPoints)) cTime
FROM matches m
LEFT JOIN stages s ON s.match_id = m.id
LEFT JOIN scores sc ON sc.stage_id = s.id
WHERE m.matchuuid = 'E13A4C61-A2B8-48E2-BE1B-1FFB77CC5849'
GROUP BY sc.alias, s.stage_name
ORDER BY s.stage_name, zeroTime, cTime
) tv
) t
GROUP BY alias
ORDER BY zeroTime, fRank, fTime;
Outputs:
+-----------+-----------+-----------+-------+-----------+
| alias | catabbr | fTime | fRank | zeroTime |
+-----------+-----------+-----------+-------+-----------+
| Back Bob | S | 141.89 | 100 | 0 |
+-----------+-----------+-----------+-------+-----------+
| Ben Scal | ES | 74.85 | 68 | 0 |
+-----------+-----------+-----------+-------+-----------+
The zeroTime column makes it so I can sort by times that are greater than 0, and correctly calculate the rank.
When I try to join them I get no results or anything. Is there a way to combine/join these two queries into one query so that they will output the following?
+-----------+-----------+-----------+-------+-----------+---------------+---------------+---------------+---------------+
| alias | catabbr | fTime | fRank | zeroTime | Stage 1 Time | Stage 1 Rank | Stage 2 Time | Stage 2 Rank |
+-----------+-----------+-----------+-------+-----------+---------------+---------------+---------------+---------------+
| Ben Scal | ES | 74.85 | 68 | 0 | 31.97 | 27 | 42.88 | 38 |
+-----------+-----------+-----------+-------+-----------+---------------+---------------+---------------+---------------+
| Back Bob | S | 141.89 | 100 | 0 | 92.98 | 54 | 48.91 | 46 |
+-----------+-----------+-----------+-------+-----------+---------------+---------------+---------------+---------------+
This will be going into a php page, so I can loop through and pull in what I need, the main thing I’m trying to do is join these two queries.
My structures and data are too big for a sqlfiddle.
Here is the structure and inserts for matches and stages: http://pastebin.com/YvZevd5j
Here is the structure and inserts for scores: http://pastebin.com/jLBYxMvc
One thing to note too, is that a game could have 1 or more stages and 1 or more particpants.
This Answer may not scale well as you would need to know the number of stages, but perhaps you can handle building it with a loop or something in you application. Here’s a simplified version, you can modify it to work with your data.