|
| 1 | +--1308. Running Total for Different Genders |
| 2 | +-- |
| 3 | +--Table: Scores |
| 4 | +-- |
| 5 | +--+---------------+---------+ |
| 6 | +--| Column Name | Type | |
| 7 | +--+---------------+---------+ |
| 8 | +--| player_name | varchar | |
| 9 | +--| gender | varchar | |
| 10 | +--| day | date | |
| 11 | +--| score_points | int | |
| 12 | +--+---------------+---------+ |
| 13 | +--(gender, day) is the primary key for this table. |
| 14 | +--A competition is held between females team and males team. |
| 15 | +--Each row of this table indicates that a player_name and with gender has scored score_point in someday. |
| 16 | +--Gender is 'F' if the player is in females team and 'M' if the player is in males team. |
| 17 | +-- |
| 18 | +-- |
| 19 | +--Write an SQL query to find the total score for each gender at each day. |
| 20 | +-- |
| 21 | +--Order the result table by gender and day |
| 22 | +-- |
| 23 | +--The query result format is in the following example: |
| 24 | +-- |
| 25 | +--Scores table: |
| 26 | +--+-------------+--------+------------+--------------+ |
| 27 | +--| player_name | gender | day | score_points | |
| 28 | +--+-------------+--------+------------+--------------+ |
| 29 | +--| Aron | F | 2020-01-01 | 17 | |
| 30 | +--| Alice | F | 2020-01-07 | 23 | |
| 31 | +--| Bajrang | M | 2020-01-07 | 7 | |
| 32 | +--| Khali | M | 2019-12-25 | 11 | |
| 33 | +--| Slaman | M | 2019-12-30 | 13 | |
| 34 | +--| Joe | M | 2019-12-31 | 3 | |
| 35 | +--| Jose | M | 2019-12-18 | 2 | |
| 36 | +--| Priya | F | 2019-12-31 | 23 | |
| 37 | +--| Priyanka | F | 2019-12-30 | 17 | |
| 38 | +--+-------------+--------+------------+--------------+ |
| 39 | +--Result table: |
| 40 | +--+--------+------------+-------+ |
| 41 | +--| gender | day | total | |
| 42 | +--+--------+------------+-------+ |
| 43 | +--| F | 2019-12-30 | 17 | |
| 44 | +--| F | 2019-12-31 | 40 | |
| 45 | +--| F | 2020-01-01 | 57 | |
| 46 | +--| F | 2020-01-07 | 80 | |
| 47 | +--| M | 2019-12-18 | 2 | |
| 48 | +--| M | 2019-12-25 | 13 | |
| 49 | +--| M | 2019-12-30 | 26 | |
| 50 | +--| M | 2019-12-31 | 29 | |
| 51 | +--| M | 2020-01-07 | 36 | |
| 52 | +--+--------+------------+-------+ |
| 53 | +--For females team: |
| 54 | +--First day is 2019-12-30, Priyanka scored 17 points and the total score for the team is 17. |
| 55 | +--Second day is 2019-12-31, Priya scored 23 points and the total score for the team is 40. |
| 56 | +--Third day is 2020-01-01, Aron scored 17 points and the total score for the team is 57. |
| 57 | +--Fourth day is 2020-01-07, Alice scored 23 points and the total score for the team is 80. |
| 58 | +--For males team: |
| 59 | +--First day is 2019-12-18, Jose scored 2 points and the total score for the team is 2. |
| 60 | +--Second day is 2019-12-25, Khali scored 11 points and the total score for the team is 13. |
| 61 | +--Third day is 2019-12-30, Slaman scored 13 points and the total score for the team is 26. |
| 62 | +--Fourth day is 2019-12-31, Joe scored 3 points and the total score for the team is 29. |
| 63 | +--Fifth day is 2020-01-07, Bajrang scored 7 points and the total score for the team is 36. |
| 64 | + |
| 65 | +--# Write your MySQL query statement below |
| 66 | +select s1.gender, s1.day, sum(s2.score_points) as total from Scores s1, Scores s2 |
| 67 | +where s1.gender = s2.gender and s1.day >= s2.day |
| 68 | +group by s1.gender, s1.day |
| 69 | +order by s1.gender, s1.day |
0 commit comments