回复: 16
跳转到指定楼层
上一主题 下一主题
收起左侧

问一道SQL 题,求解答

🔗
匿名用户-JB2UN  2020-11-24 22:56:19 |倒序浏览

2021(10-12月) 分析|数据科学类 本科 其他@ - Other - Onsite  | | Other | 其他

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

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

x

问大家一道leetcode变形题,求解答??
. 1point 3acres
given table A:
+---------+------------+----------+--------+
| user_id | date       | platform | amount | ..
+---------+------------+----------+--------+
| 1
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
7-01 | desktop  | 2           |
| 2019-07-01 | mobile   | 3           |
| 2019-07-01 | both     | 1           |

上一篇:钉有趣 DA 过经
下一篇:LinkedIn DS Intern HR面
推荐
martha2 2020-11-25 12:05:45 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

推荐
tiffany345 2020-11-25 10:18:10 | 只看该作者
全局:
本帖最后由 tiffany345 于 2020-11-24 18:22 编辑

我这里用了两个CTE.
第一个里面先 left join 来判断 platform
第二个里面用了 union 来合并所有的可能性
..
根据 test case 提供的数据可以在 MySQL 里面得到如下结果:

. 1point 3 acres
  1. WITH CTE1 as   (
  2. SELECT DISTINCT
  3. a.user_id,
  4. a.date,
  5. case . 1point 3acres
  6. when a.platform = 'mobile' and b.platform is null then 'mobile'
  7. when a.platform = 'desktop' and b.platform is null then 'desktop' ..
  8. when a.platform is not null and b.platform is not null then 'both'
  9. else null end as 'platform'

  10. FROM platform a LEFT JOIN platform b
  11. ON a.user_id = b.user_id
  12. AND a.date = b.date
  13. AND a.platform <> b.platform
  14. ),
  15. . 1point3acres
  16. CTE2 as   (.1point3acres
  17. select user_id, date, platform from CTE1
  18. union. check 1point3acres for more.
  19. select user_id, date, 'mobile' as platform from CTE1 where platform = 'both'
  20. union. Waral dи,
  21. select user_id, date, 'desktop' as platform from CTE1 where platform = 'both'
  22. )


  23. SELECT
  24. date,
  25. platform,
  26. count(user_id) as total_users

  27. FROM CTE2
  28. GROUP BY date, platform. 1point3acres
  29. ORDER BY date, platform desc
  30. ;
复制代码



本帖子中包含更多资源

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

x
回复

使用道具 举报

全局:
方法比较笨,但是应该有效

  1. with a as (
  2. select date, user_id, sum(case when platform='mobile' then amount else 0 end ) as ma,sum(case when platform='desktop' then amount else 0 end) as da from A group by date, user),
  3. ref as (select distinct date, 'mobile' as platform from A union (select distinct date,'desktop' as platform from A) union select distinct date,'both' as platform from A),
  4. b as (select date, count(case when ma>0 and da>0 then userid else null end) as bothn, count(case when ma=0 and da>0 then userid else null end) as dn, count(case when da=0 and ma>0 then userid else null end) as mn from a group by date)
  5. select date, platform, coalesce(total_users, 0) as total_users from (
  6. select date, 'both' as p , bothn as total_users from b where bothn >0 union select date, 'mobile' as p, mn as total_users from b where mn>0 union select date,  'desktop' as p ,dn as total_users from b where dn>0) sub right join ref on sub.date=ref.date,sub.p=ref.platform
复制代码
回复

使用道具 举报

全局:
combine rows,要用for, 再用一个case when
回复

使用道具 举报

全局:
Group by date, count user id. Having date = 2019-7-1

评分

参与人数 1大米 +1 收起 理由
charlie029 + 1 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
Qomo 2020-11-25 02:19:12 | 只看该作者
全局:
cloverlyy 发表于 2020-11-25 01:36
Group by date, count user id. Having date = 2019-7-1

这题最终结果应该不只2019-07-01的
回复

使用道具 举报

全局:
踩我的朋友,要是有5个platform 你会scale吗?不写for你来做一个?count(userid) = 2你能知道是哪2个platform?莫名其妙
回复

使用道具 举报

🔗
cloverlyy 2020-11-25 04:02:47 | 只看该作者
全局:
Qomo 发表于 2020-11-25 02:19
这题最终结果应该不只2019-07-01的

嗯嗯嗯,我的答案不对。 积分不够,看不了全文。不知道答案中both是什么。。。
回复

使用道具 举报

🔗
Qomo 2020-11-25 07:06:00 | 只看该作者
全局:
cloverlyy 发表于 2020-11-25 04:02
嗯嗯嗯,我的答案不对。 积分不够,看不了全文。不知道答案中both是什么。。。

both 应该是指 each user_id 既有 mobile transaction 也有 desktop transaction.
回复

使用道具 举报

🔗
lijason1030 2020-11-25 07:55:12 | 只看该作者
全局:
能想到的最简单的方法是用union。desktop和mobile的用group by就可以,后面both的可以self join一下date+user_id并且左边取desktop右边取mobile,两个都不是null就是1,然后sum一下,最后两个table union all起来。
回复

使用道具 举报

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

本版积分规则

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