中级农民
- 积分
- 112
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-8-29
- 最后登录
- 1970-1-1
|
本帖最后由 crystalcc 于 2019-6-30 16:13 编辑
Facebook Marketplace 这个题在地里面经多次出现,然而以我的积分能看到的十分有限……感谢此贴的面经和讨论,收获良多 https://www.1point3acres.com/bbs ... science-481715.html. check 1point3acres for more.
(再次跪求各位,如果本帖对你们有帮助,求加米呀!)
Session table
Date | sessionid | userid | action (enter/click/send/exit)
Time table (sessionid都是unique的)
Date | Sessionid | time_spent (s)
Q1: Average sessions/user per day within the last 30 days
find the #sessions per day per userid, filter data in the last 30 days, then avg
SELECT date, COUNT(DISTINCT sessionid)/COUNT(DISTINCT userid) as ratio
FROM session
WHERE DATEDIFF(date, CURENT_DATE()) <= 30
GROUP BY date. .и
ORDER BY date
.--
Q2: Time distribution of user 先问大概会是怎么样的一个分布。Calculate the time spent distribution on marketplace by users for a day? (x-axis is ts bucket, and y axis is number of users)
/*First calculate the total time spent for each user, build a temp table to match the time spent in the time table with its user_id in the session table, using the session_id. Then group by time spent and count how many users are in each bucket, group by date. 疑问:我认为left join是必要的,因为要average over all users regardless of him having time spent or not;然而是不是这两个table的sessionid是完全重合的呀,如果完全重合的话似乎inner join也没问题?*/
SELECT temp.total_time_spent, COUNT(DISTINCT temp.userid)
FROM (SELECT s.userid, SUM(t.time_spent) as total_time_spent
FROM session s LEFT JOIN time t
ON s.session_id = t.session_id
GROUP BY s.userid) temp
GROUP BY 1. ----
ORDER BY 1
可能的问题:session left join time,由于user同一个sessionid下可能有多个action,会导致出现大量重复sessionid。解决方案也许可以是关注一下action,比如exit action可以刨去不算。. Χ
/*另外一种理解方式来做:由于session table中sessionid是primary key, 先用sessionid来group by time spent,最后join回time表*/
SELECT temp.total_time_spent, COUNT(DISTINCT user_id)
FROM time LEFT JOIN . ----
(SELECT sessionid, SUM(time_spent) as total_time_spent
FROM session
GROUP BY sessionid) temp
ON t.sessionid = temp.sessionid. check 1point3acres for more.
GROUP BY 1
ORDER BY 1
可能的问题:把所有user都视为一样的了,然而比如新用户和老用户在time spent上会有很大不同。
求有心的小伙伴们共同讨论此题!!
|
|