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