Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8392707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:35:02+00:00 2026-06-09T19:35:02+00:00

I currently have two really complex queries that I use to bring in scores

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:35:03+00:00Added an answer on June 9, 2026 at 7:35 pm

    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.

    SELECT 
      alias,
      catAbbr,
      sum(fTime) as fTime,
      sum(fRank) as fRank,
      sum(zeroTime) as zeroTime,
      sum(Stage_1_Time) as stage_1_Time,
      sum(Stage_1_Rank) as Stage_1_Rank,
      sum(Stage_2_Time) as stage_2_Time,
      sum(Stage_2_Rank) as Stage_2_Rank
    FROM (
      SELECT 
        alias,
        catAbbr,
        0 as fTime,
        0 as fRank,
        0 as zeroTime,
        Stage_1_Time as stage_1_Time,
        Stage_1_Rank as Stage_1_Rank,
        0 as stage_2_Time,
        0 as Stage_2_Rank
      FROM (query that gets stage 1 info)
      UNION
      SELECT
        alias,
        catAbbr,
        0 as fTime,
        0 as fRank,
        0 as zeroTime,
        0 as stage_1_Time,
        0 as Stage_1_Rank,
        stage_2_Time as stage_2_Time,
        Stage_2_Rank as Stage_2_Rank
      FROM (query that gets stage 2 info)
      UNION
      SELECT
        alias,
        catAbbr,
        fTime as fTime,
        fRank as fRank,
        zeroTime as zeroTime,
        0 as stage_1_Time,
        0 as Stage_1_Rank,
        0 as stage_2_Time,
        0 as Stage_2_Rank
      FROM (query that gets averaged info)
    ) temp
    GROUP BY alias, catAbbt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I (currently) have two forms, one that needs to call the other. In other
Currently I have two tests that each individually test for a button_click within a
I currently have two anchor tags sending url queries to put votes inside a
I have two tables in APEX that are linked by their primary key. One
I currently have two comboboxes on a form. The first combobox contains a list
This is really two questions, noted below: Currently I have some public internal helper
I currently have two tables similar to users and programs that are linked through
I have two shared libraries in Linux that when built are identical in all
I currently have two different models: User and Project . The User model has
I currently have two SQL commands. One retrieves a list of unique IDs from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.