LeetCode:Database 61.市场分析 II

Posted Xiao Miao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode:Database 61.市场分析 II相关的知识,希望对你有一定的参考价值。

要求:写一个 SQL 查询确定每一个用户按日期顺序卖出的第二件商品的品牌是否是他们最喜爱的品牌。如果一个用户卖出少于两件商品,查询的结果是 no 。

题目保证没有一个用户在一天中卖出超过一件商品

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 是该表的主键

Users 表:

+---------+------------+----------------+
| 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 表:

+----------+------------+---------+----------+-----------+
| 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 表:

+---------+------------+
| 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                 |
+-----------+--------------------+

SQL语句:

#1.方法1
WITH d AS (
SELECT b.user_id AS id,b.favorite_brand AS f1,c.item_brand AS f2,
row_number() over(PARTITION BY a.seller_id ORDER BY a.order_date ASC) AS r
,COUNT(a.item_id) over(PARTITION BY a.seller_id) AS c
FROM orders a
RIGHT JOIN users b 
ON a.seller_id=b.user_id
LEFT JOIN items c
ON c.item_id=a.item_id)


SELECT id AS seller_id,'no' AS 2nd_item_fav_brand FROM d WHERE c<2
UNION ALL
SELECT id AS seller_id,CASE WHEN  f1=f2 THEN 'yes' 
ELSE 'no' END AS 2nd_item_fav_brand
FROM d WHERE r=2 AND c>=2;
#2.方法2
WITH d AS (
SELECT b.user_id AS id1,b.favorite_brand AS f1,c.item_brand AS f2,
row_number() over(PARTITION BY a.seller_id ORDER BY a.order_date ASC) AS r
FROM orders a
RIGHT JOIN users b 
ON a.seller_id=b.user_id
LEFT JOIN items c
ON c.item_id=a.item_id)

SELECT DISTINCT id1 AS seller_id,IF(id1 IN (SELECT id1  FROM d WHERE r=2 AND f1=f2)
 ,'yes','no') AS 2nd_item_fav_brand FROM d;

以上是关于LeetCode:Database 61.市场分析 II的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode:Database 47.销售分析III

LeetCode:Database 46.销售分析 II

LeetCode:Database 09.游戏玩法分析 I

LeetCode:Database 45.销售分析 I

LeetCode:Database 10. 游戏玩法分析 II

LeetCode:Database 39.产品销售分析 I