📣 4th of July限时特惠: VIP通行证立减$68
查看: 1357| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 请教一题SQL leetcode 550

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

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

  1. SELECT ROUND(COUNT(DISTINCT a1.player_id) / (SELECT COUNT(DISTINCT player_id) FROM Activity), 2) AS fraction
  2. FROM Activity AS a1, Activity AS a2
  3. WHERE a1.player_id = a2.player_id
  4.     AND DATEDIFF(a1.event_date, a2.event_date) = -1
  5.     AND (a1.player_id, a1.event_date) IN
  6.         (SELECT player_id, MIN(event_date)
  7.         FROM Activity
  8.         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
  1. 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
  2. from Activity as t1 inner join
  3.     (select player_id, min(event_date) as first_event
  4.     from Activity
  5.     group by player_id) as t2
  6. on t1.player_id = t2.player_id
复制代码


我的问题:
3. 这里的inner join 没明白。这里t1和t2做 inner join的话,不就是t2自己吗?



上一篇:分享一下5月份面试国内几家大公司的面试题,有找国内工作的同学可以参考下
下一篇:Remove brackets from an algebraic string 问题
全局:
event date不一样

补充内容 (2020-9-19 04:32):
第一个solution的分子是连续的distinct player,是从cross join的那个表,分母是所有distinct player,从activity那个表

评分

参与人数 1大米 +2 收起 理由
cherry5284 + 2 赞一个!

查看全部评分

回复

使用道具 举报

🔗
qldx 2020-9-19 05:28:10 | 只看该作者
全局:
按照我的理解
1. 第一个是必须要用a1.,要不然的话后面两个表a1, a2里都有player_id无法区分,而(SELECT COUNT(DISTINCT player_id) FROM Activity), 2)这个子查询只是为了算出分母而已,子查询内没有重名的担忧
2. 是的,a1,a2是主要的两个表,用来选择出满足符合条件的行,而作为分母的那个表其实只是为了得到分母而已,相对独立

3. 不是,t2是选择以后的,相当于每一个player只有一行了,而t1是原表,连接以后的表的尺寸大概是相当于t1原表的基础上增加了一列作为first_event(注明:也就是说同一个player,多个event_date使用同样的first_event),然后在这个基础上进行case when

评分

参与人数 1大米 +2 收起 理由
cherry5284 + 2 谢谢

查看全部评分

回复

使用道具 举报

全局:
分清一下,join都是两个表在做行为。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表