-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3384-TeamDominanceByPassSuccess.sql
More file actions
128 lines (121 loc) · 5.14 KB
/
3384-TeamDominanceByPassSuccess.sql
File metadata and controls
128 lines (121 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
-- 3384. Team Dominance by Pass Success
-- Table: Teams
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | player_id | int |
-- | team_name | varchar |
-- +-------------+---------+
-- player_id is the unique key for this table.
-- Each row contains the unique identifier for player and the name of one of the teams participating in that match.
-- Table: Passes
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | pass_from | int |
-- | time_stamp | varchar |
-- | pass_to | int |
-- +-------------+---------+
-- (pass_from, time_stamp) is the primary key for this table.
-- pass_from is a foreign key to player_id from Teams table.
-- Each row represents a pass made during a match, time_stamp represents the time in minutes (00:00-90:00) when the pass was made,
-- pass_to is the player_id of the player receiving the pass.
-- Write a solution to calculate the dominance score for each team in both halves of the match. The rules are as follows:
-- A match is divided into two halves: first half (00:00-45:00 minutes) and second half (45:01-90:00 minutes)
-- The dominance score is calculated based on successful and intercepted passes:
-- When pass_to is a player from the same team: +1 point
-- When pass_to is a player from the opposing team (interception): -1 point
-- A higher dominance score indicates better passing performance
-- Return the result table ordered by team_name and half_number in ascending order.
-- The result format is in the following example.
-- Example:
-- Input:
-- Teams table:
-- +------------+-----------+
-- | player_id | team_name |
-- +------------+-----------+
-- | 1 | Arsenal |
-- | 2 | Arsenal |
-- | 3 | Arsenal |
-- | 4 | Chelsea |
-- | 5 | Chelsea |
-- | 6 | Chelsea |
-- +------------+-----------+
-- Passes table:
-- +-----------+------------+---------+
-- | pass_from | time_stamp | pass_to |
-- +-----------+------------+---------+
-- | 1 | 00:15 | 2 |
-- | 2 | 00:45 | 3 |
-- | 3 | 01:15 | 1 |
-- | 4 | 00:30 | 1 |
-- | 2 | 46:00 | 3 |
-- | 3 | 46:15 | 4 |
-- | 1 | 46:45 | 2 |
-- | 5 | 46:30 | 6 |
-- +-----------+------------+---------+
-- Output:
-- +-----------+-------------+-----------+
-- | team_name | half_number | dominance |
-- +-----------+-------------+-----------+
-- | Arsenal | 1 | 3 |
-- | Arsenal | 2 | 1 |
-- | Chelsea | 1 | -1 |
-- | Chelsea | 2 | 1 |
-- +-----------+-------------+-----------+
-- Explanation:
-- First Half (00:00-45:00):
-- Arsenal's passes:
-- 1 → 2 (00:15): Successful pass (+1)
-- 2 → 3 (00:45): Successful pass (+1)
-- 3 → 1 (01:15): Successful pass (+1)
-- Chelsea's passes:
-- 4 → 1 (00:30): Intercepted by Arsenal (-1)
-- Second Half (45:01-90:00):
-- Arsenal's passes:
-- 2 → 3 (46:00): Successful pass (+1)
-- 3 → 4 (46:15): Intercepted by Chelsea (-1)
-- 1 → 2 (46:45): Successful pass (+1)
-- Chelsea's passes:
-- 5 → 6 (46:30): Successful pass (+1)
-- The results are ordered by team_name and then half_number
-- CREATE TABLE If not exists Teams (
-- player_id INT,
-- team_name VARCHAR(100)
-- )
-- CREATE TABLE if not exists Passes (
-- pass_from INT,
-- time_stamp VARCHAR(5),
-- pass_to INT
-- )
-- Truncate table Teams
-- insert into Teams (player_id, team_name) values ('1', 'Arsenal')
-- insert into Teams (player_id, team_name) values ('2', 'Arsenal')
-- insert into Teams (player_id, team_name) values ('3', 'Arsenal')
-- insert into Teams (player_id, team_name) values ('4', 'Chelsea')
-- insert into Teams (player_id, team_name) values ('5', 'Chelsea')
-- insert into Teams (player_id, team_name) values ('6', 'Chelsea')
-- Truncate table Passes
-- insert into Passes (pass_from, time_stamp, pass_to) values ('1', '00:15', '2')
-- insert into Passes (pass_from, time_stamp, pass_to) values ('2', '00:45', '3')
-- insert into Passes (pass_from, time_stamp, pass_to) values ('3', '01:15', '1')
-- insert into Passes (pass_from, time_stamp, pass_to) values ('4', '00:30', '1')
-- insert into Passes (pass_from, time_stamp, pass_to) values ('2', '46:00', '3')
-- insert into Passes (pass_from, time_stamp, pass_to) values ('3', '46:15', '4')
-- insert into Passes (pass_from, time_stamp, pass_to) values ('1', '46:45', '2')
-- insert into Passes (pass_from, time_stamp, pass_to) values ('5', '46:30', '6')
-- Write your MySQL query statement below
SELECT
t1.team_name,
IF(time_stamp <= '45:00', 1, 2) AS half_number, -- 判断时间是上半场还是下半场
SUM(IF(t1.team_name = t2. team_name, 1, -1)) AS dominance -- 优势得分
FROM
Passes AS p
JOIN
Teams AS t1 ON pass_from = t1.player_id -- Passes表与Teams表连接 确定pass_from的队伍
JOIN
Teams AS t2 ON pass_to = t2.player_id -- Passes表与Teams表连接 确定pass_to的队伍
GROUP BY
t1.team_name, IF(time_stamp <= '45:00', 1, 2) -- 分组 pass_from 的队伍,和场次
ORDER BY
t1.team_name, half_number