【大数据技术】AB球队得分流水表,得到连续三次得分的队员名字
- 需求:
连续N次(及以上)为球队得分的球员名单 - 建表
CREATE TABLE basketball_detail(
team string ,
score_time string ,
name string
) row format delimited fields terminated by ',' ;
- 导入数据
insert into table basketball_detail values
('A',1,'2020/8/28 9:01:14',1,'A1'),
('A',5,'2020/8/28 9:02:28',1,'A5'),
('B',4,'2020/8/28 9:03:42',3,'B4'),
('A',4,'2020/8/28 9:04:55',3,'A4'),
('B',1,'2020/8/28 9:06:09',3,'B1'),
('A',3,'2020/8/28 9:07:23',3,'A3'),
('A',4,'2020/8/28 9:08:37',3,'A4'),
('B',1,'2020/8/28 9:09:51',2,'B1'),
('B',2,'2020/8/28 9:11:05',2,'B2'),
('B',4,'2020/8/28 9:12:18',1,'B4'),
('A',1,'2020/8/28 9:13:32',2,'A1'),
('A',1,'2020/8/28 9:14:46',1,'A1'),
('A',4,'2020/8/28 9:16:00',1,'A4'),
('B',3,'2020/8/28 9:17:14',3,'B3'),
('B',2,'2020/8/28 9:18:28',3,'B2'),
('A',2,'2020/8/28 9:19:42',3,'A2'),
('A',1,'2020/8/28 9:20:55',1,'A1'),
('B',3,'2020/8/28 9:22:09',2,'B3'),
('B',3,'2020/8/28 9:23:23',3,'B3'),
('A',5,'2020/8/28 9:24:37',2,'A5'),
('B',1,'2020/8/28 9:25:51',3,'B1'),
('B',2,'2020/8/28 9:27:05',1,'B2'),
('A',3,'2020/8/28 9:28:18',1,'A3'),
('B',4,'2020/8/28 9:29:32',1,'B4'),
('A',1,'2020/8/28 9:30:46',3,'A1'),
('B',1,'2020/8/28 9:32:00',1,'B1'),
('A',4,'2020/8/28 9:33:14',2,'A4') ;
数据如下图:
实操:
select t3.name ,t3.team ,t3.score_time ,t3.lx_num
from
(
select
t2.*
,count(*) over(partition by name,df ) lx_num
from
(
select
t1.*
,t1.rn1-t1.rn2 as df
from
(
select
t.score_time,t.name ,t.team,
row_number() over(order by score_time) rn1 ,
row_number() over(partition by name order by score_time) rn2
from basketball_detail t
)t1
)t2
)t3
where t3.lx_num >=2
最终结果如图
同理,可把2改为3求得连续3次为球队得分的球员