LeetCode MySQL 1159. 市场分析 II(rank+over)
文章目录
- 1. 题目
- 2. 解题
1. 题目
表: Users
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| user_id | int |
| join_date | date |
| favorite_brand | varchar |
+----------------+---------+
user_id 是该表的主键
表中包含一位在线购物网站用户的个人信息,用户可以在该网站出售和购买商品。
表: Orders
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| order_id | int |
| order_date | date |
| item_id | int |
| buyer_id | int |
| seller_id | int |
+---------------+---------+
order_id 是该表的主键
item_id 是 Items 表的外键
buyer_id 和 seller_id 是 Users 表的外键
表: Items
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| item_id | int |
| item_brand | varchar |
+---------------+---------+
item_id 是该表的主键
写一个 SQL 查询确定每一个用户按日期顺序卖出的第二件商品的品牌是否是他们最喜爱的品牌。
如果一个用户卖出少于两件商品,查询的结果是 no 。
题目保证没有一个用户在一天中卖出超过一件商品
下面是查询结果格式的例子:
Users table:
+---------+------------+----------------+
| user_id | join_date | favorite_brand |
+---------+------------+----------------+
| 1 | 2019-01-01 | Lenovo |
| 2 | 2019-02-09 | Samsung |
| 3 | 2019-01-19 | LG |
| 4 | 2019-05-21 | HP |
+---------+------------+----------------+Orders table:
+----------+------------+---------+----------+-----------+
| order_id | order_date | item_id | buyer_id | seller_id |
+----------+------------+---------+----------+-----------+
| 1 | 2019-08-01 | 4 | 1 | 2 |
| 2 | 2019-08-02 | 2 | 1 | 3 |
| 3 | 2019-08-03 | 3 | 2 | 3 |
| 4 | 2019-08-04 | 1 | 4 | 2 |
| 5 | 2019-08-04 | 1 | 3 | 4 |
| 6 | 2019-08-05 | 2 | 2 | 4 |
+----------+------------+---------+----------+-----------+Items table:
+---------+------------+
| item_id | item_brand |
+---------+------------+
| 1 | Samsung |
| 2 | Lenovo |
| 3 | LG |
| 4 | HP |
+---------+------------+Result table:
+-----------+--------------------+
| seller_id | 2nd_item_fav_brand |
+-----------+--------------------+
| 1 | no |
| 2 | yes |
| 3 | yes |
| 4 | no |
+-----------+--------------------+id 为 1 的用户的查询结果是 no,因为他什么也没有卖出
id为 2 和 3 的用户的查询结果是 yes,因为他们卖出的第二件商品的品牌是他们自己最喜爱的品牌
id为 4 的用户的查询结果是 no,因为他卖出的第二件商品的品牌不是他最喜爱的品牌
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/market-analysis-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
- 连接各表,窗口函数求出排名
select o.order_date, o.seller_id, i.item_brand, u.favorite_brand,rank() over(partition by o.seller_id order by o.order_date) rnk
from Orders o
left join Users u
on o.seller_id = u.user_id
left join Items i
on o.item_id = i.item_id
{"headers": ["order_date", "seller_id", "item_brand", "favorite_brand", "rnk"],
"values": [["2019-08-01", 2, "HP", "Samsung", 1], ["2019-08-04", 2, "Samsung", "Samsung", 2], ["2019-08-02", 3, "Lenovo", "LG", 1], ["2019-08-03", 3, "LG", "LG", 2], ["2019-08-04", 4, "Samsung", "HP", 1], ["2019-08-05", 4, "Lenovo", "HP", 2]]}
- 选出 rnk = 2 的,再左连接 所有用户的 id
# Write your MySQL query statement below
select u.seller_id, if(item_brand is null, 'no', case when favorite_brand = item_brand then 'yes'else 'no' end) as 2nd_item_fav_brand
from
(select user_id as seller_idfrom Users
) u
left join
(select *from(select o.order_date, o.seller_id, i.item_brand, u.favorite_brand,rank() over(partition by o.seller_id order by o.order_date) rnkfrom Orders o left join Users uon o.seller_id = u.user_idleft join Items ion o.item_id = i.item_id) t1where rnk = 2
) t2
on u.seller_id = t2.seller_id
我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
LeetCode MySQL 1159. 市场分析 II(rank+over)相关推荐
- LeetCode MySQL 1158. 市场分析 I
文章目录 1. 题目 2. 解题 1. 题目 Table: Users +----------------+---------+ | Column Name | Type | +----------- ...
- LeetCode MySQL 1083. 销售分析 II
文章目录 1. 题目 2. 解题 1. 题目 Table: Product +--------------+---------+ | Column Name | Type | +----------- ...
- LeetCode MySQL解题目录
已完成的 LeetCode MySQL 数据库题目.点击查看我的 LeetCode 算法解题目录. 已解决 123/123 - 简单 54 中等 51 困难 18 前置入门学习 MySQL 基本查询. ...
- LeetCode MySQL 1454. 活跃用户(连续dense_rank排名函数)
文章目录 1. 题目 2. 解题 1. 题目 表 Accounts: +---------------+---------+ | Column Name | Type | +------------- ...
- Leetcode - Subsets I,II
Leetcode - 078. Subsets 这道题重定义了什么叫可行解: 一般而言,可行解需要满足强约束性条件集,而本题的可行解就是单一弱约束性条件(distinct integers,只需要当前 ...
- LeetCode MySQL 1308. 不同性别每日分数总计(累加/变量/窗口函数)
文章目录 1. 题目 2. 解题 1. 题目 表: Scores +---------------+---------+ | Column Name | Type | +--------------- ...
- Leetcode - Permutations I,II
Leetcode - 046 Permutations 全排列问题是回溯的典型例题: 1.可行解的组成形式是给定数组中的所有数的组合,故而大小上可以作为可行解判定条件 2.每次需要在剩下可被选中的集合 ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- 【DFS + 记忆化递归】LeetCode 140. Word Break II
LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...
最新文章
- qt chart 如何使网格填满整个窗口_Qt实现截屏的关键函数
- 找工作,我该如何是好?
- android开启前台服务_如何在PC端一次性开启Tasker的所有权限和服务
- Java高级面试题!java编程思想怎么学
- mysql中IFNULL,IF,CASE的区别
- 线程创建 pthread_create 中自定义参数注意事项
- Android数据的四种存储方式
- httpServletRequest中的流只能读取一次的原因
- AndroidLinux gdb用法
- Python爬虫爬取VIP网站
- 《金字塔原理》读书笔记
- SCM供应链协同管理系统解决方案
- !! A股历史平均市盈率走势图
- DevExpress VCL Subscription 版本:21.1.5
- 【CSDN软件工程师能力认证学习精选】SQL语句查询
- 立体视觉入门指南:对级约束与Fusiello法极线校正
- 触动的话语,为自己而活
- CCF201409-1相邻数对(C语言)
- easy-table-vue+VueJs、SpringBoot+Mybatis实现MVVM模型前后台数据交互
- An internal routine has requested a dump of selected redo.This usually happens following a specific