LeetCode-1308. Different sex daily total score (moderate)

Table: Scores

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| PLAYER_NAME | VARCHAR |
| Gender | VARCHAR |
| Day | DATE |
| score_points | int |
+ --------------- + --- + ------
(gender, Day) is the table's primary key
match was held between women's and men's team
in each row of the table represents a named (player_name) sex (gender) entry who one day received (score_points) score
if the participants are female, then gender as 'F', if the participants are male, then gender as 'M'
 

Write a SQL statement to query each sex in each day's total score, according to gender and date sorting query results

The following are examples of query results format:

Scores表:
+-------------+--------+------------+--------------+
| 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           |
+ -------- + ------------ + ------------- + ------------- - +
results table:
+ -------- + ------- + ------------ +
| Gender | Day | Total |
+ ------ - + ------------ + ------- +
| F | 2019-12-30 | 17 |
| F | 2019-12-31 | 40 |
| F | 2020 -01-01 | 57 is |
| F. | 2020-01-07 | 80 |
| M | 2019-12-18 | 2 |
| M | 2019-12-25 | 13 is |
| M | 2019-12-30 | 26 is |
| M | 2019-12-31 | 29 |
| M | 2020-01-07 | 36 |
+ -------- + ------------ + ---- --- +
female team:
the first day is 2019-12-30, Priyanka get 17 points, the team's total score is 17 points
the next day is 2019-12-31, Priya get 23 points, the team's total score is 40 points
on the third day is 2020-01-01, Aron get 17 points, the team's total score is 57 points
The fourth day is 2020-01-07, Alice get 23 points, the team's total score is 80 points
male teams:
the first day of 2019-12-18, Jose get 2 points, the team's total score is 2 points
the next day is 2019-12-25, Khali get 11 points, the team's total score is 13 points in
the third day of 2019-12-30, Slaman get 13 points, the team's total score is 26 points
fourth day 2019-12-31 , Joe won three points, the score is 29 points ranks
fifth day was 2020-01-07, Bajrang 7 points, team score is 36 points

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/running-total-for-different-genders
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Moderation: Write a SQL statement to query each sex in each day's total score, according to gender and date sorting query results

Thoughts: sorted by date, then the cumulative results of men and women.

Problem solving:

Method a: connecting the two tables, of the same sex, the date of the table greater than or equal left and right sheet date
of the same sex, the summation of all scores for that day and date of the day before.

SELECT s1.gender, s1.day, SUM(s2.score_points) total
FROM Scores s1, Scores s2
WHERE s1.gender = s2.gender AND s1.day >= s2.day
GROUP BY s1.gender, s1.day
ORDER BY s1.gender, s1.day
-- 练习
-- sql执行顺序是先分组,然后一条一条通过where判断。
SELECT s1.gender, s1.day, SUM(s2.score_points) total
from scores s1, scores s2
-- 性别相同,s1日期再s2日期之前
where s1.gender = s2.dender AND s1.day >= s2.day
-- 按照性别和日期分组
group by s1.gender,s1.day
order by s1.gender,s1.day

Method two: two new variables
1. press gender, day ordering 

SELECT gender, day
FROM Scores
ORDER BY gender, day

2. Add two variables:
the first variable is used to record the cumulative score @total;
second variable @pre_gender to gender prior to recording a record.
If a record with the same sex before sex, cumulative score; gender and gender if not the same as the previous record, the score is reset.

SELECT gender, day, 
       CASE WHEN @pre_gender = gender THEN @total := @total + score_points
            ELSE @total := score_points
       END AS total,   
       @pre_gender := gender
FROM Scores,
(SELECT @total := 0, @pre_gender := NULL) as init
ORDER BY gender, day
-- 练习
select gender,day,
-- 通过判断性别,如果相同就在total结果加上本行
case when @pre_gender = gender then @total := @total + score_points
-- 如果性别不同,就直接赋值给total,因为最先按照性别排序,所以性别不同,就是一个性别结束了。
else @total := score_points end as total,
--更新gender值
@pre_gender :=gender
feom scores,
-- 定义变量
(select @total :=0, @pre_gender := NULL) as init
ORDER BY gender,day

3. The above-obtained new table as the table need, return gender, day, total

SELECT gender, day, total
FROM (SELECT gender, day, 
            CASE WHEN @pre_gender = gender THEN @total := @total + score_points
                 ELSE @total := score_points
                 END AS total,   
            @pre_gender := gender
      FROM Scores,
      (SELECT @total := 0, @pre_gender := NULL) as init
      ORDER BY gender, day) AS need

 Knowledge points:

Published 144 original articles · won praise 2 · Views 5734

Guess you like

Origin blog.csdn.net/Hello_JavaScript/article/details/104782479