How do I calculate ratios in SQL?

前端 未结 2 1669
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 08:48

Suppose I have a GAME table with the following fields

user_id, result

I wish to calculate win percentage defined as total numb

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-03 09:10

    In MySQL, you can use this shortcut:

    SELECT user_id, avg(Result = 'Win') AS WinPercent
    FROM Game
    GROUP BY user_id
    ORDER BY WinPercent DESC
    

    Assuming Result is never NULL (empty string is ok). Otherwise you have to use Result IS NOT NULL AND Result = 'Win'

提交回复
热议问题