【MySQL】LeetCode 534&550

mysql> select * from activity
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date   | games_played |
+-----------+-----------+------------+--------------+
|         1 |         2 | 2016-03-01 |            5 |
|         1 |         2 | 2016-05-02 |            6 |
|         2 |         3 | 2017-06-25 |            1 |
|         3 |         1 | 2016-03-02 |            0 |
|         3 |         4 | 2018-07-03 |            5 |
+-----------+-----------+------------+--------------+
5 rows in set (0.00 sec)

534 游戲玩法分析3

查詢玩家在該日期之前所玩的游戲場數(shù)
mysql> select player_id,event_date,sum(games_played)over(partition by player_id order by event_date) as games_played_so_far
    -> from activity
    -> order by player_id,event_date;
+-----------+------------+---------------------+
| player_id | event_date | games_played_so_far |
+-----------+------------+---------------------+
|         1 | 2016-03-01 |                   5 |
|         1 | 2016-05-02 |                  11 |
|         2 | 2017-06-25 |                   1 |
|         3 | 2016-03-02 |                   0 |
|         3 | 2018-07-03 |                   5 |
+-----------+------------+---------------------+
5 rows in set (0.00 sec)

550 游戲玩法分析4

mysql> select * from activity
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date   | games_played |
+-----------+-----------+------------+--------------+
|         1 |         2 | 2016-03-01 |            5 |
|         1 |         2 | 2016-03-02 |            6 |
|         2 |         3 | 2017-06-25 |            1 |
|         3 |         1 | 2016-03-02 |            0 |
|         3 |         4 | 2018-07-03 |            5 |
+-----------+-----------+------------+--------------+
5 rows in set (0.00 sec)
mysql> select round(count(distinct player_id)/(select count(distinct player_id)from activity ),2) as fraction
    -> from( select player_id,event_date,
    -> lead(event_date) over(partition by player_id)as next_log_date
    -> from activity ) T 
    -> where next_log_date is not null and datediff(next_log_date,event_date)=1;
+----------+
| fraction |
+----------+
|     0.33 |
+----------+
1 row in set (0.00 sec)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容