–1 建表插入数据 create table if not exists haokan_ads( user_id int, user_type int, day1 DATETIME , play_rate double, resource string); SELECT * from haokan_ads order by play_rate desc LIMIT 100; insert into haokan_ads VALUES (1, 1 ,‘2021-01-02 00:00:00’, 0.6,‘ads1’); insert into haokan_ads VALUES (2 ,1,‘2021-01-08 00:00:00’, 0.9 , ‘ads2’); insert into haokan_ads VALUES (3 ,0, ‘2021-01-03 00:00:00’ ,0.52, ‘ads3’); insert into haokan_ads VALUES (4 ,1, ‘2021-01-07 00:00:00’, 0.62 ,‘ads4’); insert into haokan_ads VALUES (5 ,1 ,‘2021-01-11 00:00:00’,0.19 ,‘ads5’); insert into haokan_ads VALUES (6 ,0 ,‘2021-01-02 00:00:00’,0.18 ,‘ads6’); insert into haokan_ads VALUES (7 ,1 ,‘2021-01-02 00:00:00’, 0.49, ‘ads7’); insert into haokan_ads VALUES (8 ,0, ‘2021-01-03 00:00:00’, 0.39, ‘ads8’); insert into haokan_ads VALUES (9 ,0 ,‘2021-01-09 00:00:00’, 0.21, ‘ads9’); insert into haokan_ads VALUES (10 ,0, ‘2021-01-03 00:00:00’, 0.39, ‘ads10’); insert into haokan_ads VALUES (11 ,0 ,‘2021-01-04 00:00:00’, 0.25, ‘ads11’); insert into haokan_ads VALUES (12, 0 ,‘2021-01-03 00:00:00’, 0.35, ‘ads12’); insert into haokan_ads VALUES (13 ,0 ,‘2021-01-09 00:00:00’, 0.1, ‘ads13’); –2 需求 每个自然周,新用户,完播率排名前5的用户的网页跳转来源? SELECT temp_table.week_of_year, temp_table.user_id, temp_table.RESOURCE FROM ( SELECT WEEKOFYEAR(day1) week_of_year, user_id, RESOURCE ,dense_rank() OVER(PARTITION by WEEKOFYEAR(day1) ORDER BY play_rate DESC) AS orderrank FROM haokan_ads WHERE user_type = 1 ) temp_table WHERE orderrank < 6 ; 
|