当前位置:网站首页>AB team score flow chart, get the names of the players who score three consecutive times and the names of the players who catch up with and surpass the opponents each time (PDD)
AB team score flow chart, get the names of the players who score three consecutive times and the names of the players who catch up with and surpass the opponents each time (PDD)
2022-07-23 20:30:00 【South Lake Fishing Song】
AB Team scoring table , The name of the player who scored three consecutive times And the names of the players who catch up with their opponents every time (pdd)
I found something similar to the original question , This is the most difficult question I encountered in the interview
problem : The two basketball teams had a fierce basketball game , The score went up alternately . After the game , You have a detailed list of the scores of the two teams , Recorded the team team, Player number number, The name of the player name, Score score score And scoring time scoretime(datetime). Now the team will reward the outstanding players in the game , So please use sql According to the statistics
1) Three consecutive times ( And above ) A list of players who score for the team
-- Consider the whole of the two teams as a continuous time :
select distinct name
from
(
select *
,row_number()over(order by score_time asc) as rank1
,row_number()over(partition by `team`,`number` order by score_time asc ) rank2
,(row_number()over(order by score_time asc) - row_number()over(partition by `team`,`number` order by score_time asc )) as diff
from basketball_game_score_detail
)t
group by diff,name
having count(1) >=3 ;
-- Consider continuously from the time of your team :
# My solution idea :
select distinct name
from
(
select *
,row_number()over(partition by team order by score_time asc) as rank1
,row_number()over(partition by team,`number` order by score_time asc ) rank2
,(row_number()over(partition by team order by score_time asc) -
row_number()over(partition by `team`,`number` order by score_time asc )) as diff
from basketball_game_score_detail
)t
group by diff,name
having count(1) >=3 ;
# Another author's solution idea is also possible :
select distinct a.name ,a.team from
(
select *
,lead(name,1) over(partition by team order by score_time) as ld1
,lead(name,2) over(partition by team order by score_time) as ld2
,lag(name,1) over(partition by team order by score_time) as lg1
,lag(name,2) over(partition by team order by score_time) as lg2
from basketball_game_score_detail
) a
where (a.name = a.ld1 and a.name = a.ld2) -- case1: Compare the current record with the next two records
or (a.name = a.ld1 and a.name = a.lg1) -- case2: Compare the current record with the previous record and the next record
or (a.name = a.lg1 and a.name = a.lg2);-- case3: Compare the current record with the previous two records
-- where a.name =a.ld1 or a.name=a.lg1 ; If adjacent 3 Below
2) Name and corresponding time of players who help their teams to surpass the score in the game .
select team
,`number`
,score_time
,name
,A_score_acum
,B_score_acum
,score_gap
,last_score_gap
,score_gap * last_score_gap as product
from
(
select *
,(A_score_acum - B_score_acum) as score_gap
,lag(A_score_acum-B_score_acum,1)over(order by score_time) as last_score_gap
from
(
select *
,case when team='A' then score else 0 end as A_score
,case when team='B' then score else 0 end as B_score
,sum(case when team='A' then score else 0 end)over(order by score_time) as A_score_acum
,sum(case when team='B' then score else 0 end)over(order by score_time) as B_score_acum
from basketball_game_score_detail
) t1
)t2
where score_gap != 0 # Switch to (A_score_acum - B_score_acum) <> 0 Yes
and score_gap * last_score_gap <= 0; # It has to include 0
experimental data :
CREATE TABLE basketball_game_score_detail(
team VARCHAR(40) NOT NULL ,
number VARCHAR(100) NOT NULL,
score_time datetime NOT NULL,
score int NOT NULL,
name varchar(100) NOT NULL
);
insert into basketball_game_score_detail values('A',1,'2020/8/28 9:01:14',1,'A1');
insert into basketball_game_score_detail values('A',5,'2020/8/28 9:02:28',1,'A5');
insert into basketball_game_score_detail values('B',4,'2020/8/28 9:03:42',3,'B4');
insert into basketball_game_score_detail values('A',4,'2020/8/28 9:04:55',3,'A4');
insert into basketball_game_score_detail values('B',1,'2020/8/28 9:06:09',3,'B1');
insert into basketball_game_score_detail values('A',3,'2020/8/28 9:07:23',3,'A3');
insert into basketball_game_score_detail values('A',4,'2020/8/28 9:08:37',3,'A4');
insert into basketball_game_score_detail values('B',1,'2020/8/28 9:09:51',2,'B1');
insert into basketball_game_score_detail values('B',2,'2020/8/28 9:11:05',2,'B2');
insert into basketball_game_score_detail values('B',4,'2020/8/28 9:12:18',1,'B4');
insert into basketball_game_score_detail values('A',1,'2020/8/28 9:13:32',2,'A1');
insert into basketball_game_score_detail values('A',1,'2020/8/28 9:14:46',1,'A1');
insert into basketball_game_score_detail values('A',1,'2020/8/28 9:14:59',1,'A1');
insert into basketball_game_score_detail values('A',4,'2020/8/28 9:16:00',1,'A4');
insert into basketball_game_score_detail values('B',3,'2020/8/28 9:17:14',3,'B3');
insert into basketball_game_score_detail values('B',2,'2020/8/28 9:18:28',3,'B2');
insert into basketball_game_score_detail values('A',2,'2020/8/28 9:19:42',3,'A2');
insert into basketball_game_score_detail values('A',1,'2020/8/28 9:20:55',1,'A1');
insert into basketball_game_score_detail values('B',3,'2020/8/28 9:22:09',2,'B3');
insert into basketball_game_score_detail values('B',3,'2020/8/28 9:23:23',3,'B3');
insert into basketball_game_score_detail values('A',5,'2020/8/28 9:24:37',2,'A5');
insert into basketball_game_score_detail values('B',1,'2020/8/28 9:25:51',3,'B1');
insert into basketball_game_score_detail values('B',2,'2020/8/28 9:27:05',1,'B2');
insert into basketball_game_score_detail values('A',3,'2020/8/28 9:28:18',1,'A3');
insert into basketball_game_score_detail values('B',4,'2020/8/28 9:29:32',1,'B4');
insert into basketball_game_score_detail values('A',1,'2020/8/28 9:30:46',3,'A1');
insert into basketball_game_score_detail values('B',1,'2020/8/28 9:32:00',1,'B1');
insert into basketball_game_score_detail values('A',4,'2020/8/28 9:33:14',2,'A4');
insert into basketball_game_score_detail values('B',1,'2020/8/28 9:34:28',1,'B1');
insert into basketball_game_score_detail values('B',5,'2020/8/28 9:35:42',2,'B5');
insert into basketball_game_score_detail values('A',1,'2020/8/28 9:36:55',1,'A1');
insert into basketball_game_score_detail values('B',1,'2020/8/28 9:38:09',3,'B1');
insert into basketball_game_score_detail values('A',1,'2020/8/28 9:39:23',3,'A1');
insert into basketball_game_score_detail values('B',2,'2020/8/28 9:40:37',3,'B2');
insert into basketball_game_score_detail values('A',3,'2020/8/28 9:41:51',3,'A3');
insert into basketball_game_score_detail values('A',1,'2020/8/28 9:43:05',2,'A1');
insert into basketball_game_score_detail values('B',3,'2020/8/28 9:44:18',3,'B3');
insert into basketball_game_score_detail values('A',5,'2020/8/28 9:45:32',2,'A5');
insert into basketball_game_score_detail values('B',5,'2020/8/28 9:46:46',3,'B5');
https://mp.weixin.qq.com/s/PfOFMGJeomIfEMT2Atg6Xw( Challenge complete )
边栏推荐
- 如何给电脑系统重置系统?方法其实很简单
- QT下assimp库的模型加载
- After the input error of next numerical data type () occurs, it can still be input normally next time
- What antenna is used for ant interface_ There is an interface at the back of the TV that says standard ant 75 Euro input. What does it mean, antenna? Can you connect the closed route "Suggested collec
- 华泰证券低佣金开户链接安全吗,怎么办理低佣金
- 数组——977. 有序数组的平方
- 考研 | 高等数学 Chapter4 不定积分
- Leetcode 228. summary interval (yes, solved)
- Chinese [easy to understand] cannot be set when installing SVN localization package
- 17. Life cycle
猜你喜欢
随机推荐
The latest version of conflict+docker+mysql8 deployment tutorial
Leetcode 228. summary interval (yes, solved)
Data warehouse 4.0 notes - data warehouse environment construction - DataGrid preparation and data preparation
17.生命周期
What if there is no word document in win11? There is no word document solution tutorial in win11
使用高德地图JS API 2.0加载起点终点路径轨迹
EXCEL的密码相关
Choice is greater than effort! Guiyang campus Xiaoge 0 foundation successfully transferred to software testing and gained 12K!
平安证券低佣金开户链接安全吗,怎么办理低佣金
el-upload实现上传文件预览
牛客C基础题练习
今日睡眠质量记录81分
美团大脑百亿级知识图谱的构建及应用进展
“脉”向未来!华为云MRS助力脉脉迁移平滑上云
剑指 Offer II 115. 重建序列
数组——27. 移除元素
微软网站上关于在Edge浏览器中打开或关闭smartScreen的说明有误
使用Jmeter和VisualVW进行压测准备
dokcer镜像理解
从ACL 2022 Onsite经历看NLP热点









