中级农民
- 积分
- 106
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2020-3-8
- 最后登录
- 1970-1-1
|
注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
力扣 SQL 题目答案 按题型分类:
(I) JOIN (不包括 self join)
后续更新的部分目录:
新人求米,关注等更!
Average Selling Price
本质就是把同一产品的不同价格与相应的销量相乘, 除以总销量, 得到该产品的平均价格.
- select p.product_id, round( sum(p.price * u.units)/sum(u.units),2) as average_price
- from Prices p join UnitsSold u
- on p.product_id = u.product_id
- and u.purchase_date between p.start_date and p.end_date
- group by p.product_id;
复制代码
Product Sales Analysis I
就是一个简单的 left join.
- select product_name, year, price
- from Sales s left join Product p
- on s.product_id = p.product_id;
- # similar but quicker
- Select a.product_name, b.year, b.price
- from product a join sales b
- on a.product_id = b.product_id
- order by b.year
复制代码
* Students and Examinations
- # 这个相对较快, 吧Students 和Subjects 先 join 起来了, 然后座位一个整体, 再去join 另一个
- select a.student_id,a.student_name,a.subject_name, count(b.subject_name) attended_exams
- from (select student_id,student_name,subject_name from Students,Subjects ) a
- left join examinations b
- on a.student_id = b.student_id and a.subject_name = b.subject_name
- group by a.student_id,a.student_name,a.subject_name
- order by a.student_id
复制代码
- # 比较慢的做法, 用了 cross join, 不太常用
- select a.student_id,a.student_name,a.subject_name,coalesce(count(e.subject_name)) as attended_exams from
- (select student_id,student_name,s.subject_name
- from subjects s cross join students st) a left join examinations e
- on a.student_id=e.student_id and a.subject_name=e.subject_name
- group by 1,2,3
- order by 1
复制代码
*Game Play Analysis IV
- select round(count(a2.player_id)/count(a1.player_id),2) as fraction
- from
- (select player_id,
- min(event_date) as first_login
- from activity
- group by player_id) a1
- left join activity a2
- on a1.player_id = a2.player_id
- and datediff(a2.event_date, a1.first_login)=1
- # easier to understand
- SELECT ROUND(SUM(CASE WHEN a.event_date + 1 = b.event_date THEN 1 ELSE 0 END)/COUNT(DISTINCT a.player_id), 2) AS fraction
- FROM (SELECT player_id, MIN(event_date) AS event_date
- FROM Activity
- GROUP BY player_id) AS a JOIN Activity AS b
- ON a.player_id = b.player_id;
复制代码
{% hint style="info" %} subquery 提取出来了每个用户的第一次 login {% endhint %}
*Active Businesses
- select business_id
- from Events e join
- (select event_type, avg(occurences) as avg from Events group by event_type) a
- on e.event_type = a.event_type and occurences > a.avg
- group by business_id
- having count(distinct e.event_type) > 1
- # 审题问题, 不是每个event_type 至少有一个大于平均的, 而是business_id 至少有一个大于平均的.
复制代码
*Reported Posts II
- select round(avg(t.num),2) average_daily_percent from (
- select count(distinct R.post_id)/count(distinct A.post_id)*100 num
- from Actions A left join Removals R on A.post_id=R.post_id
- where extra='spam'
- group by action_date
- ) t
复制代码
Market Analysis I
- with cte as(select count(distinct order_id) as count, buyer_id
- from Orders
- where order_date like '2019%'
- group by 2)
- select user_id as buyer_id, join_date, ifnull(cte.count,0) as orders_in_2019
- from Users u left join cte
- on u.user_id = cte.buyer_id
- # 直接写效果不好的时候, 试试 cte!
- # 更简单可用 CASE WHEN
- select u.user_id as buyer_id, join_date,
- sum(case when year(order_date)=2019 then 1 else 0 end) orders_in_2019
- from Users u left join Orders o
- on u.user_id=o.buyer_id
- group by u.user_id
- # OR
- SELECT u.user_id AS buyer_id, u.join_date, COUNT(DISTINCT o.order_id) AS orders_in_2019
- FROM Users u LEFT JOIN (SELECT * FROM Orders WHERE year(order_date) = 2019) o
- ON u.user_id = o.buyer_id
- GROUP BY 1
复制代码 {% hint style="warning" %} 直接选择日期中 YEAR 可以用 YEAR(order_date) = 2019 {% endhint %}
NPV Queries
- select q.id, q.year, ifnull(n.npv,0) as npv
- from Queries q left join NPV n
- on q.id=n.id and q.year=n.year
- order by 1 asc
复制代码
|
 组图打开中,请稍候......
上一篇: 坐标加拿大🇨🇦 | DS/MLE方向跳槽 | 刷题+ML基础下一篇: 500 Data相关的题
|