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.
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):
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)toSUM(IF(Amount > 0, Amount, 0))in both places it occurs in the query.