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 3990180
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:27:47+00:00 2026-05-20T06:27:47+00:00

Suppose I have a GAME table with the following fields user_id, result I wish

  • 0

Suppose I have a GAME table with the following fields

user_id, result

I wish to calculate win percentage defined as total number of records where

result = 'Win'

Compared to total number of records where

result <> ''

How can I do this in SQL and have the result returned in order with highest win percentage first?

  • 1 1 Answer
  • 2 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-05-20T06:27:48+00:00Added an answer on May 20, 2026 at 6:27 am

    Most database systems should be able to handle this with something akin to:

    Select user_id
        , Sum( Case When Result = 'Win' Then 1 Else 0 End ) / Count(*) * 1.0 As WinPercent
    From Game
    Group By user_id
    Order By WinPercent Desc
    

    Btw, I’m assuming you meant to say the win percentage is the total number of wins out of the total number of games. If you really did mean where result <> '', then you would need to modify the query like so:

    Select user_id
        , Sum( Case When Result = 'Win' Then 1 Else 0 End ) 
            / Sum( Case When Result <> '' Then 1 Else 0 End ) * 1.0 As WinPercent
    From Game
    Group By user_id
    Order By WinPercent Desc
    

    Addition

    It was suggested in comments that this is SQL Server specific. It is not. Everything in the above solutions is part of the SQL standard. Whether a given database system implements all of the features required is obviously another matter. So, if we are switching gears to finding a solution that would actually work on most database systems, the one feature that has the most variance in support would likely be the use of the column alias in the Order By. You can achieve the equivalent in a couple of ways but one way that would work on most database systems would be to use a derived table:

    Select Z.user_id, Z.WinPercent
    From    (
            Select user_id
                , Sum( Case When Result = 'Win' Then 1 Else 0 End ) 
                    / Sum( Case When Result <> '' Then 1 Else 0 End ) * 1.0 As WinPercent
            From Game
            Group By user_id
            ) As Z
    Order By Z.WinPercent Desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a GAME table with two fields user_id, win win = 1
Suppose I have the following table GAME match_id, user_id, score 1, 10, 45 1,
Suppose I have the following tables: CREATE TABLE Game ( GameID INT UNSIGNED NOT
Suppose we have the following table data: ID parent stage submitted 1 1 1
Suppose I have a table with following data: gameId difficultyLevel numberOfQuestions -------------------------------------------- 1 1
Suppose I have following data in my game. I have developed game in cocos2d.
Suppose I have the following HTML: <form id=myform> <input type='checkbox' name='foo[]'/> Check 1<br/> <input
Support we have an n * m table, and two players play this game.
Suppose I have a game written in C/C++. For simplicity, I have: struct masterStruct
I have a table rating with these fields rate_id, game_id, rating, ip . Let

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.