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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:31:52+00:00 2026-05-25T19:31:52+00:00

I have shoot ’em game where users compete against each other over the course

  • 0

I have shoot ’em game where users compete against each other over the course of a week to accumulate the most points. I want to write a query that aggregates statistical data from the shots table. The tables and relationships of concern here are:

  • user has many competition_periods
  • competition_period belongs to user
  • competition_period has many shots
  • shot belongs to competition_period

In the shots table I have the following fields to work with:

  • result –> string values: WON, LOST or TIED
  • amount_won –> integer values: e.g., -100, 0, 2000, etc.

For each user, I want to return a result set with the following aggregated stats:

  • won_count
  • lost_count
  • tied_count
  • total_shots_count (won_count + lost_count + tied_count)
  • total_amount_won (sum of amount_won)
  • avg_amount_won_per_shot (total_amount_won / total_shots_count)

I’ve worked on this query for few hours now, but haven’t made much headway. The statistical functions trip me up. A friend suggested that I try to return the results in a new virtual table called shot_records.

  • 1 1 Answer
  • 0 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-25T19:31:53+00:00Added an answer on May 25, 2026 at 7:31 pm

    Here is the basic solution, computing the statistics across all shots for a given player (you didn’t specify if you want them on a per-competition-period basis or not):

     SELECT user, SUM(IF(result = 'WON', 1, 0))  AS won_count, 
                  SUM(IF(result = 'LOST', 1, 0)) AS lost_count, 
                  SUM(IF(result = 'TIED', 1, 0)) AS tied_count, 
                  COUNT(*)                       AS total_shots_count, 
                  SUM(amount_won)                AS total_amount_won, 
                  (SUM(amount_won) / COUNT(*))   AS avg_amount_won_per_shot 
     FROM user U INNER JOIN competition_periods C ON U.user_id = C.user_id
     INNER JOIN shots S ON C.competition_period_id = S.competition_period_id
     GROUP BY user
    

    Note that this includes negatives in calculating the “total won” figure (that is, the total is decreased by losses). If that’s not the correct algorithm for your game, you would change SUM(Amount) to SUM(IF(Amount > 0, Amount, 0)) in both places it occurs in the query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a game that is fairly sizeable, 30-40-50 people in at most points
I made shoot em up like game.But I have only one ememy which fallows
I am trying to trouble shoot a JUnit. In the source code, I have
Greetings... I come in peace, shoot to kill... I have a container of type
I have WPF app that processes a lot of urls (thousands), each it sends
I have a character in an iPod Touch game I am making that moves
I have a little archer game I'm working on, and previously in my code
I'm being really ambitious and working on a 2D Shoot 'em Up game that
I have a key down event attached to one of my textboxes and over
I have a server with two different network interfaces, each with a different IP

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.