There is an activity table as follows
player_id device_id event_date games_played
1 2 '2016-01-01' 5
1 2 '2016-05-02' 6
1 3 '2017-07-25' 1
3 1 '2016-03-02' 0
3 4 '2018-07-03' 5
Where games_played is the number of games that players log in to play,
Query how many games each player has played every day? The results are as follows:
player_id event_date games_played_so_far
1 '2016-01-01' 5
1 '2016-05-02' 11
1 '2017-07-25' 12
3 '2016-03-02' 0
3 '2018-07-03' 5
Explanation: Player 1 played 5 for the first time, so it is 5, and the second time is 6, so the total is 5+6=11, and the
third time is 1, the total is 5+6+1=12
Player 2 similar
solve:
select player_id
,event_date
,sum(games_played) over(partition by player_id order by event_date) as games_played_so_far
from activity
;
player_id event_date games_played_so_far
1 2016-01-01 5
1 2016-05-02 11
1 2017-07-25 12
3 2016-03-02 0
3 2018-07-03 5
备注:建表和数据
create table activity(player_id int,device_id int,event_date date,games_played int);
insert into activity values(1,2,'2016-01-01',5);
insert into activity values(1,2,'2016-05-02',6);
insert into activity values(1,3,'2017-07-25',1);
insert into activity values(3,1,'2016-03-02',0);
insert into activity values(3,4,'2018-07-03',5);