I have a table where I want to add a new column based on the result.
If I create the following query it works correctly as all the tables appear plus a new call RESULT
SELECT ID,
CASE
WHEN RESULT_HOME = RESULT_VISIT THEN "D"
WHEN RESULT_HOME < RESULT_VISIT THEN "W2"
WHEN RESULT_HOME > RESULT_VISIT THEN "W1"
END AS RESULT
FROM matches;
But how do I add the ‘CASE’ in the following query? It fails in this example:
SELECT
TEAM
CASE
WHEN RESULT_HOME = RESULT_VISIT THEN "D"
WHEN RESULT_HOME < RESULT_VISIT THEN "W2"
WHEN RESULT_HOME > RESULT_VISIT THEN "W1"
END AS RESULT
FROM
(
SELECT HOME AS TEAM, RESULT_HOME AS RESULT FROM matches
UNION ALL
SELECT VISIT AS TEAM, RESULT_VISIT AS RESULT FROM matches
)
AS s
GROUP BY
TEAM;
The error is:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CASE
WHEN RESULT_HOME = RESULT_VISIT THEN "D"
WHEN RESULT_HOME < RESULT_VI...' at line 3
2
Answers
It looks to me, that the query is correct, you’re just missing collon after TEAM.
So
should work.
If we can assume:
Demo dbfiddle.uk