0

So say I have a mysql statement like this:

  SELECT username, @n := @n + 1 ranking, `1st places`, `2nd places`, `3rd places`, `top5`, `top3
FROM
(
SELECT username,
SUM(CASE WHEN rating = 1 THEN 1 ELSE 0 END) `1st places`,
SUM(CASE WHEN rating = 2 THEN 1 ELSE 0 END) `2nd places`,
SUM(CASE WHEN rating = 3 THEN 1 ELSE 0 END) `3rd places`,
SUM(CASE WHEN rating < 6 THEN 5 ELSE 0 END) `top5`,
SUM(CASE WHEN rating < 4 THEN 5 ELSE 0 END) `top3`
FROM Table1
GROUP BY username
ORDER BY `1st places` DESC
) q, (SELECT @n := 0) n

How can I add up the columns that I created called, '1st places' + '2nd places' + '3rd places' in this sql statement and create another column called "total finishes"? Seems easy, but I cannot seem to figure it out.

3
  • I don't quite get it. rating < 4 overlaps with rating = 1 and so does partially with rating < 6. Can you give some example data with expected output so see what you want to do? Commented Jul 1, 2013 at 20:00
  • so what I want is a point system. Example: say someone places 1st aka "rating=1" then they get a point for being in 1st, then they also get 5 points for being in the top 5 (aka rating < 6) and also get 5 more points for being in the top 3 (aka rating < 4) so they would get a total of 11 points. Commented Jul 1, 2013 at 20:07
  • Im realizing how dumb this is actually now that I typed all of that, but I guess is there a way to do that? Even tho thinking about it, I dont need it lol. Commented Jul 1, 2013 at 20:08

2 Answers 2

1

You can just add them together and give an alias to that extra column:-

SELECT username, @n := @n + 1 ranking, `1st places`, `2nd places`, `3rd places`, `top5`, `top3, `1st places` + `2nd places` + `3rd places` AS `total finishes`
FROM
(
SELECT username,
SUM(CASE WHEN rating = 1 THEN 1 ELSE 0 END) `1st places`,
SUM(CASE WHEN rating = 2 THEN 1 ELSE 0 END) `2nd places`,
SUM(CASE WHEN rating = 3 THEN 1 ELSE 0 END) `3rd places`,
SUM(CASE WHEN rating < 6 THEN 5 ELSE 0 END) `top5`,
SUM(CASE WHEN rating < 4 THEN 5 ELSE 0 END) `top3`
FROM Table1
GROUP BY username
ORDER BY `1st places` DESC
) q, (SELECT @n := 0) n
Sign up to request clarification or add additional context in comments.

Comments

0

I'm pretty sure that you can't re-use a field with a calculated value in the same SELECT query, however wrapping into an additional sub-query may be an option:

SELECT username, @n := @n + 1 ranking, `1st places`, `2nd places`, `3rd places`, `top5`, `top3
FROM
(
    SELECT
    *,
    `1st places`+`2nd places`+`3rd places` `total finishes`
    FROM (
        SELECT username,
        SUM(CASE WHEN rating = 1 THEN 1 ELSE 0 END) `1st places`,
        SUM(CASE WHEN rating = 2 THEN 1 ELSE 0 END) `2nd places`,
        SUM(CASE WHEN rating = 3 THEN 1 ELSE 0 END) `3rd places`,
        SUM(CASE WHEN rating < 6 THEN 5 ELSE 0 END) `top5`,
        SUM(CASE WHEN rating < 4 THEN 5 ELSE 0 END) `top3`
        FROM Table1
        GROUP BY username
        ORDER BY `1st places` DESC
    )
)
q, (SELECT @n := 0) n

Haven't tried, still...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.