注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
550是prime的题目,我就找了题目来做。
题目是这样的
Write an SQL query that reports the fraction of players that logged in again on the day after the day they first logged in, rounded to 2 decimal places. In other words, you need to count the number of players that logged in for at least two consecutive days starting from their first login date, then divide that number by the total number of players.
The query result format is in the following example:
Activity table:
+-----------+-----------+------------+--------------+
| 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 |
+-----------+-----------+------------+--------------+
Result table:
+-----------+
| fraction |
+-----------+
| 0.33 |
+-----------+
我在网上看到2个solution, 都有些迷糊。恳请大牛回答。
第一个solution
- SELECT ROUND(COUNT(DISTINCT a1.player_id) / (SELECT COUNT(DISTINCT player_id) FROM Activity), 2) AS fraction
- FROM Activity AS a1, Activity AS a2
- WHERE a1.player_id = a2.player_id
- AND DATEDIFF(a1.event_date, a2.event_date) = -1
- AND (a1.player_id, a1.event_date) IN
- (SELECT player_id, MIN(event_date)
- FROM Activity
- GROUP BY player_id)
复制代码
我的问题是
1. 请问COUNT(DISTINCT a1.player_id) 和SELECT COUNT(DISTINCT player_id) FROM Activity 有什么区别? 就算用了alias, 也好像都是用的activity这张表,没有进行过变动。
2. 还是和第一行有关,是不是这个solution使用了3个表,其中两个activity用了两个alias?
第二个solution使用的是inner join
- select round(sum(case when t1.event_date = t2.first_event+1 then 1 else 0 end)/count(distinct t1.player_id), 2) as fraction
- from Activity as t1 inner join
- (select player_id, min(event_date) as first_event
- from Activity
- group by player_id) as t2
- on t1.player_id = t2.player_id
复制代码
我的问题:
3. 这里的inner join 没明白。这里t1和t2做 inner join的话,不就是t2自己吗?
|