返回介绍

solution / 1300-1399 / 1308.Running Total for Different Genders / README_EN

发布于 2024-06-17 01:03:21 字数 3614 浏览 0 评论 0 收藏 0

1308. Running Total for Different Genders

中文文档

Description

Table: Scores

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| player_name   | varchar |
| gender    | varchar |
| day       | date  |
| score_points  | int   |
+---------------+---------+
(gender, day) is the primary key (combination of columns with unique values) for this table.
A competition is held between the female team and the male 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 the female team and 'M' if the player is in the male team.

 

Write a solution to find the total score for each gender on each day.

Return the result table ordered by gender and day in ascending order.

The result format is in the following example.

 

Example 1:

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    | 2019-12-25 | 11       |
| Slaman    | M    | 2019-12-30 | 13       |
| Joe     | M    | 2019-12-31 | 3      |
| Jose    | M    | 2019-12-18 | 2      |
| Priya     | F    | 2019-12-31 | 23       |
| Priyanka  | F    | 2019-12-30 | 17       |
+-------------+--------+------------+--------------+
Output: 
+--------+------------+-------+
| gender | day    | total |
+--------+------------+-------+
| F    | 2019-12-30 | 17  |
| F    | 2019-12-31 | 40  |
| F    | 2020-01-01 | 57  |
| F    | 2020-01-07 | 80  |
| M    | 2019-12-18 | 2   |
| M    | 2019-12-25 | 13  |
| M    | 2019-12-30 | 26  |
| M    | 2019-12-31 | 29  |
| M    | 2020-01-07 | 36  |
+--------+------------+-------+
Explanation: 
For the female team:
The first day is 2019-12-30, Priyanka scored 17 points and the total score for the team is 17.
The second day is 2019-12-31, Priya scored 23 points and the total score for the team is 40.
The third day is 2020-01-01, Aron scored 17 points and the total score for the team is 57.
The fourth day is 2020-01-07, Alice scored 23 points and the total score for the team is 80.

For the male team:
The first day is 2019-12-18, Jose scored 2 points and the total score for the team is 2.
The second day is 2019-12-25, Khali scored 11 points and the total score for the team is 13.
The third day is 2019-12-30, Slaman scored 13 points and the total score for the team is 26.
The fourth day is 2019-12-31, Joe scored 3 points and the total score for the team is 29.
The fifth day is 2020-01-07, Bajrang scored 7 points and the total score for the team is 36.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT
  gender,
  day,
  SUM(score_points) OVER (
    PARTITION BY gender
    ORDER BY gender, day
  ) AS total
FROM Scores;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文