1159 市场分析II
SQL架构
Create table If Not Exists Users_1159 (user_id int, join_date date, favorite_brand varchar(10));
Create table If Not Exists Orders_1159 (order_id int, order_date date, item_id int, buyer_id int, seller_id int);
Create table If Not Exists Items_1159 (item_id int, item_brand varchar(10));
Truncate table Users_1159;
insert into Users_1159 (user_id, join_date, favorite_brand) values ('1', '2019-01-01', 'Lenovo');
insert into Users_1159 (user_id, join_date, favorite_brand) values ('2', '2019-02-09', 'Samsung');
insert into Users_1159 (user_id, join_date, favorite_brand) values ('3', '2019-01-19', 'LG');
insert into UseUsers_1159rs (user_id, join_date, favorite_brand) values ('4', '2019-05-21', 'HP');
Truncate table Orders_1159;
insert into Orders_1159 (order_id, order_date, item_id, buyer_id, seller_id) values ('1', '2019-08-01', '4', '1', '2');
insert into Orders_1159 (order_id, order_date, item_id, buyer_id, seller_id) values ('2', '2019-08-02', '2', '1', '3');
insert into Orders_1159 (order_id, order_date, item_id, buyer_id, seller_id) values ('3', '2019-08-03', '3', '2', '3');
insert into Orders_1159 (order_id, order_date, item_id, buyer_id, seller_id) values ('4', '2019-08-04', '1', '4', '2');
insert into Orders_1159 (order_id, order_date, item_id, buyer_id, seller_id) values ('5', '2019-08-04', '1', '3', '4');
insert into Orders_1159 (order_id, order_date, item_id, buyer_id, seller_id) values ('6', '2019-08-05', '2', '2', '4');
Truncate table Items_1159;
insert into Items_1159 (item_id, item_brand) values ('1', 'Samsung');
insert into Items_1159 (item_id, item_brand) values ('2', 'Lenovo');
insert into Items_1159 (item_id, item_brand) values ('3', 'LG');
insert into Items_1159 (item_id, item_brand) values ('4', 'HP');
表: 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,因为他卖出的第二件商品的品牌不是他最喜爱的品牌
解题
思路:先使用内连接将三个表连接起来,使用窗口函数根据seller_id分组,根据order_date排序,再使用子查询找到日期为第二的,并且卖家卖出的商品品牌与自己最喜爱的品牌一样的seller_id,使用if函数判断并且可以添加为新列,最后还要考虑订单数量少于2的卖家,直接与Users表左连接即可。
select t5.user_id as seller_id, ifnull(2nd_item_fav_brand, 'no') as 2nd_item_fav_brand from users_1159 t5 left join (
select seller_id, if(item_brand=favorite_brand,'yes', 'no') as 2nd_item_fav_brand
from(
select
seller_id, item_brand, favorite_brand,
rank() over (partition by seller_id order by order_date) as date_order
from users_1159 t1, orders_1159 t2, items_1159 t3 where t1.user_id=t2.seller_id and t2.item_id=t3.item_id
) t4
where date_order=2
) t6
on t5.user_id=t6.seller_id;