+--------------+---------+ | Column Name | Type | +--------------+---------+ | player_id | int | | device_id | int | | event_date | date | | games_played | int | +--------------+---------+
SELECT player_id,device_id FROM Activity WHERE (player_id, event_date) IN ( SELECT player_id, min(event_date) first_login FROM Activity GROUP BY player_id );
SELECT a1.player_id,a1.event_date,sum(a2.games_played) games_played_so_far FROM activity a1,activity a2 WHERE a1.player_id=a2.player_id AND a1.event_date>=a2.event_date GROUP BY a1.player_id,a1.event_date ORDER BY player_id,event_date
Result table: +-----------+ | fraction | +-----------+ | 0.33 | +-----------+ 只有 ID 为 1 的玩家在第一天登录后才重新登录,所以答案是 1/3 = 0.33
解决方案
1 2 3 4
SELECT ROUND(sum(case when datediff(a.event_date,b.first_date)=1 then 1 else 0 end)/(select count(distinct(player_id)) from activity),2) as fraction from activity a, (select player_id,min(event_date) first_date from activity group by player_id) b where a.player_id=b.player_id