Running Total for Different Genders LeetCode Solution

This Leetcode problem Running Total for Different Genders LeetCode Solution is done in SQL.

List of all LeetCode Solution

Running Total for Different Genders LeetCode Solution

Running Total for Different Genders LeetCode Solution

Problem Statement

Column NameType
player_namevarchar
gendervarchar
daydate
score_pointsint
Table: Scores

(gender, day) is the primary key for this table.
A competition is held between females team and males team.
Each row of this table indicates that a player_name and with gender has scored score_point in someday.
Gender is ‘F’ if the player is in females team and ‘M’ if the player is in males team.

Write an SQL query to find the total score for each gender at each day. Order the result table by gender and day.

The result format is in the following example.

Example 1:
Input:

player_namegenderday score_points
Aron F2020-01-0117
Alice F2020-01-0723
BajrangM2020-01-077
KhaliM2019-12-2511
SlamanM2019-12-3013
JoeM2019-12-313
JoseM2019-12-182
PriyaF2019-12-3123
PriyankaF2019-12-3017
Scores table:

Output:

genderdaytotal
F2019-12-3017
F2019-12-3140
F2020-01-0157
F2020-01-0780
M2019-12-182
M2019-12-2513
M2019-12-3026
M2019-12-3129
M2020-01-0736

Explanation:
For females team:
First day is 2019-12-30, Priyanka scored 17 points and the total score for the team is 17.
Second day is 2019-12-31, Priya scored 23 points and the total score for the team is 40.
Third day is 2020-01-01, Aron scored 17 points and the total score for the team is 57.
Fourth day is 2020-01-07, Alice scored 23 points and the total score for the team is 80.

For males team:
First day is 2019-12-18, Jose scored 2 points and the total score for the team is 2.
Second day is 2019-12-25, Khali scored 11 points and the total score for the team is 13.
Third day is 2019-12-30, Slaman scored 13 points and the total score for the team is 26.
Fourth day is 2019-12-31, Joe scored 3 points and the total score for the team is 29.
Fifth day is 2020-01-07, Bajrang scored 7 points and the total score for the team is 36.

Running Total for Different Genders LeetCode Solution MySQL

select 
  s1.gender, 
  s1.day, 
  sum(s2.score_points) as total 
from 
  Scores as s1 
  join Scores as s2 on s1.gender = s2.gender 
  and s1.day >= s2.day 
group by 
  s1.gender, 
  s1.day 
order by 
  gender, 
  day;Code language: SQL (Structured Query Language) (sql)
Scroll to Top