Results 1 to 3 of 3

Thread: Help with SQL statement to calculate a statistic

  1. #1

    Help with SQL statement to calculate a statistic

    Sorry if this question is in the wrong place, as it doesn't deal with stats in the distributed computing sense. I figured it would be a good place to post my question as it involves figuring out how to calculate a stat, which you guys are good at! Feel free to move or delete it if needed.

    I am making a card game my family likes to play and am now trying to develop the stats system. I want to show various stats but the one I'm having problems with is showing a players win/loss ratio against other players.

    I have a table called GameScores that has these columns: GameNumber, PlayerID, Score, and WonLost. I need to figure out which games two particular players have both played in. Then out of those games find how many times each player has won. From there its easy to figure out the win/loss ratio.

    I was hoping to get the SQL to do this down to one statement, but I don't know if that's possible. Here's what I came up with so far for PlayerIDs 1 and 2:

    SELECT PlayerID
    FROM GameScores
    WHERE WonLost = TRUE
    AND GameNumber IN
    (SELECT DISTINCT GameNumber FROM GameScores WHERE GameNumber IN
    (SELECT GameNumber FROM GameScores WHERE PlayerID = 1) AND GameNumber IN
    (SELECT GameNumber FROM GameScores WHERE PlayerID = 2))

    That gives me a list of the winners of the games where both players 1 and 2 have played in. Then I would have to count how many times each player has won, not counting the games where a 3rd player has won. I would love to be able to figure it out and store it in another table all in one statement but I don't know if that's possible. I've only been working with SQL for a few months.

    If anyone has any ideas, I would sure appreciate it.

  2. #2
    Stats Developer magnav0x's Avatar
    Join Date
    Mar 2002
    Location
    Dallas, TX
    Posts
    1,747
    Is this in PHP? I would do something along the lines of a while loop. I'm not sure you will be able to accomplish what you want with one SQL statment, but if you can you will need to use the COUNT() function in SQL. If you get stuck let me know and I can help you out with some php/sql stuff to get you where you want to be.
    Warning this Post is Rated "M" for Mature

    -Contains Harsh Language
    -L337 HaX0r W3RD2!
    -Partial Nudity

    I haven't lost my mind; it's backed up on tape drive somewhere.

  3. #3
    Hi magnav0x. No, I'm creating this in VB.NET. It's the only language I know so far.

    I was primarily interested to see if it could be done strictly in SQL. Looks like that won't happen. I think I figured out how to do it with the SQL I posted before, using the results from the SQL statement in my code and then writing the new data to the database.

    Thanks for the offer of help. I appreciate it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •