Question

I am building a database for a GameCenter type application. The following is the start of a query to build a weekly leaderboard for a specific game. A 'score' is stored as an INT in a table 'Game' however another field, 'scoreFormat' indicates how the scores should be displayed in the leaderboard. I was using a SELECT IF statement to convert INT to currency by concatenating a '£' at the start of the INT which worked fine. However now that I need multiple cases (EG time) I can't seem to get it to work. The case statement always goes into ELSE for some reason and I can't see why. Can anyone see my mistake? Thanks

SELECT userName as "User Name",
   score_timestamp as "Score Time",
    case score
        When scoreFormat = "time" Then SEC_TO_TIME(score)
        When scoreFormat = "money" Then Concat('£', score)
        Else score
    end as HighScore
    FROM ...

Thanks.

Was it helpful?

Solution

case 
   When scoreFormat = "time" Then SEC_TO_TIME(score)
   When scoreFormat = "money" Then Concat('£', score)
   Else score
end 
as HighScore

OTHER TIPS

There are two ways to use the CASE statement. You can use it as swich/case:

CASE scoreFormat
    WHEN 'time' THEN ...
    ELSE ..
END

Or you can use it with explicit expressions:

CASE
    WHEN scoreFormat = 'time' THEN ...
    ELSE ...
END

You are using a combination of the two, which does not work as you expect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top