Running Total for Different Genders - Problem
Running Total for Different Genders
You're organizing a competitive scoring system for a mixed-gender tournament! ๐ Each day, players from both the female team ('F') and male team ('M') score points in various competitions.
Your task is to calculate the cumulative running total for each gender on each day. This means you need to track how the total score builds up over time for both teams separately.
๐ Table Structure: Scores
| Column Name | Type |
|---|---|
| player_name | varchar |
| gender | varchar |
| day | date |
| score_points | int |
Key Points:
- The combination
(gender, day)forms the primary key - Gender is either 'F' (female) or 'M' (male)
- Multiple players can score on the same day, but we aggregate by gender per day
- You need to return results ordered by
genderanddayin ascending order
Goal: Calculate the running total score for each gender across all days, showing how the cumulative score grows over time.
Input & Output
example_1.sql โ Basic Tournament Scores
$
Input:
Scores table:
+-------------+--------+------------+-------------+
| player_name | gender | day | score_points|
+-------------+--------+------------+-------------+
| Aron | F | 2020-01-01 | 17 |
| Alice | F | 2020-01-07 | 23 |
| Bajrang | M | 2020-01-07 | 7 |
| Khali | M | 2020-01-07 | 18 |
| Slaman | M | 2020-01-11 | 2 |
+-------------+--------+------------+-------------+
โบ
Output:
+--------+------------+-------+
| gender | day | total |
+--------+------------+-------+
| F | 2020-01-01 | 17 |
| F | 2020-01-07 | 40 |
| M | 2020-01-07 | 25 |
| M | 2020-01-11 | 27 |
+--------+------------+-------+
๐ก Note:
Female team: Day 1 has 17 points (total: 17). Day 7 adds 23 points (running total: 17+23=40). Male team: Day 7 has 7+18=25 points (total: 25). Day 11 adds 2 points (running total: 25+2=27).
example_2.sql โ Same Day Multiple Players
$
Input:
Scores table:
+-------------+--------+------------+-------------+
| player_name | gender | day | score_points|
+-------------+--------+------------+-------------+
| John | M | 2023-01-01 | 10 |
| Jane | F | 2023-01-01 | 15 |
| Bob | M | 2023-01-01 | 20 |
| Alice | F | 2023-01-02 | 25 |
+-------------+--------+------------+-------------+
โบ
Output:
+--------+------------+-------+
| gender | day | total |
+--------+------------+-------+
| F | 2023-01-01 | 15 |
| F | 2023-01-02 | 40 |
| M | 2023-01-01 | 30 |
+--------+------------+-------+
๐ก Note:
On 2023-01-01: Female team scores 15, Male team scores 10+20=30. On 2023-01-02: Female running total becomes 15+25=40, Male team has no new scores so stays at 30.
example_3.sql โ Single Gender Tournament
$
Input:
Scores table:
+-------------+--------+------------+-------------+
| player_name | gender | day | score_points|
+-------------+--------+------------+-------------+
| Sarah | F | 2023-01-01 | 30 |
| Emma | F | 2023-01-02 | 20 |
| Lisa | F | 2023-01-03 | 25 |
+-------------+--------+------------+-------------+
โบ
Output:
+--------+------------+-------+
| gender | day | total |
+--------+------------+-------+
| F | 2023-01-01 | 30 |
| F | 2023-01-02 | 50 |
| F | 2023-01-03 | 75 |
+--------+------------+-------+
๐ก Note:
Only female players participate. Running totals: Day 1: 30, Day 2: 30+20=50, Day 3: 50+25=75. This tests edge case where only one gender is present.
Constraints
- 1 โค Number of rows in Scores table โค 1000
- 1 โค score_points โค 1000
- gender is either 'F' or 'M'
- day is a valid date string in format 'YYYY-MM-DD'
- The combination (gender, day) is unique in the table
Visualization
Tap to expand
Understanding the Visualization
1
Daily Scores Collection
Each day, players compete and their scores are recorded with gender and date
2
Gender Partitioning
Separate the scores into male (M) and female (F) partitions
3
Chronological Processing
Process each gender's scores in date order to maintain running totals
4
Cumulative Display
Display running totals showing how each team's score grows over time
Key Takeaway
๐ฏ Key Insight: SQL window functions with PARTITION BY gender and ORDER BY day allow efficient calculation of running totals in a single pass, making this an optimal O(n log n) solution.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code