|
| 1 | +# [2922. Market Analysis III](https://leetcode.cn/problems/market-analysis-iii) |
| 2 | + |
| 3 | +[English Version](/solution/2900-2999/2922.Market%20Analysis%20III/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>Table: <code>Users</code></p> |
| 10 | + |
| 11 | +<pre> |
| 12 | ++----------------+---------+ |
| 13 | +| Column Name | Type | |
| 14 | ++----------------+---------+ |
| 15 | +| seller_id | int | |
| 16 | +| join_date | date | |
| 17 | +| favorite_brand | varchar | |
| 18 | ++----------------+---------+ |
| 19 | +seller_id is the primary key for this table. |
| 20 | +This table contains seller id, join date, and favorite brand of sellers. |
| 21 | +</pre> |
| 22 | + |
| 23 | +<p>Table: <code>Items</code></p> |
| 24 | + |
| 25 | +<pre> |
| 26 | ++---------------+---------+ |
| 27 | +| Column Name | Type | |
| 28 | ++---------------+---------+ |
| 29 | +| item_id | int | |
| 30 | +| item_brand | varchar | |
| 31 | ++---------------+---------+ |
| 32 | +item_id is the primary key for this table. |
| 33 | +This table contains item id and item brand.</pre> |
| 34 | + |
| 35 | +<p>Table: <code>Orders</code></p> |
| 36 | + |
| 37 | +<pre> |
| 38 | ++---------------+---------+ |
| 39 | +| Column Name | Type | |
| 40 | ++---------------+---------+ |
| 41 | +| order_id | int | |
| 42 | +| order_date | date | |
| 43 | +| item_id | int | |
| 44 | +| seller_id | int | |
| 45 | ++---------------+---------+ |
| 46 | +order_id is the primary key for this table. |
| 47 | +item_id is a foreign key to the Items table. |
| 48 | +seller_id is a foreign key to the Users table. |
| 49 | +This table contains order id, order date, item id and seller id.</pre> |
| 50 | + |
| 51 | +<p>Write a solution to find the <strong>top seller</strong> who has sold the highest number of<strong> unique</strong> items with a <strong>different</strong> brand than their favorite brand. If there are multiple sellers with the same highest count, return all of them.</p> |
| 52 | + |
| 53 | +<p>Return <em>the result table ordered by</em> <code>seller_id</code> <em>in <strong>ascending</strong> order.</em></p> |
| 54 | + |
| 55 | +<p>The result format is in the following example.</p> |
| 56 | + |
| 57 | +<p> </p> |
| 58 | +<p><strong class="example">Example 1:</strong></p> |
| 59 | + |
| 60 | +<pre> |
| 61 | +<strong>Input:</strong> |
| 62 | +Users table: |
| 63 | ++-----------+------------+----------------+ |
| 64 | +| seller_id | join_date | favorite_brand | |
| 65 | ++-----------+------------+----------------+ |
| 66 | +| 1 | 2019-01-01 | Lenovo | |
| 67 | +| 2 | 2019-02-09 | Samsung | |
| 68 | +| 3 | 2019-01-19 | LG | |
| 69 | ++-----------+------------+----------------+ |
| 70 | +Orders table: |
| 71 | ++----------+------------+---------+-----------+ |
| 72 | +| order_id | order_date | item_id | seller_id | |
| 73 | ++----------+------------+---------+-----------+ |
| 74 | +| 1 | 2019-08-01 | 4 | 2 | |
| 75 | +| 2 | 2019-08-02 | 2 | 3 | |
| 76 | +| 3 | 2019-08-03 | 3 | 3 | |
| 77 | +| 4 | 2019-08-04 | 1 | 2 | |
| 78 | +| 5 | 2019-08-04 | 4 | 2 | |
| 79 | ++----------+------------+---------+-----------+ |
| 80 | +Items table: |
| 81 | ++---------+------------+ |
| 82 | +| item_id | item_brand | |
| 83 | ++---------+------------+ |
| 84 | +| 1 | Samsung | |
| 85 | +| 2 | Lenovo | |
| 86 | +| 3 | LG | |
| 87 | +| 4 | HP | |
| 88 | ++---------+------------+ |
| 89 | +<strong>Output:</strong> |
| 90 | ++-----------+-----------+ |
| 91 | +| seller_id | num_items | |
| 92 | ++-----------+-----------+ |
| 93 | +| 2 | 1 | |
| 94 | +| 3 | 1 | |
| 95 | ++-----------+-----------+ |
| 96 | +<strong>Explanation:</strong> |
| 97 | +- The user with seller_id 2 has sold three items, but only two of them are not marked as a favorite. We will include a unique count of 1 because both of these items are identical. |
| 98 | +- The user with seller_id 3 has sold two items, but only one of them is not marked as a favorite. We will include just that non-favorite item in our count. |
| 99 | +Since seller_ids 2 and 3 have the same count of one item each, they both will be displayed in the output.</pre> |
| 100 | + |
| 101 | +## 解法 |
| 102 | + |
| 103 | +<!-- 这里可写通用的实现逻辑 --> |
| 104 | + |
| 105 | +**方法一:等值连接 + 分组 + 子查询** |
| 106 | + |
| 107 | +我们可以使用等值连接,将 `Orders` 表和 `Users` 表按照 `seller_id` 进行连接,接着再按照 `item_id` 连接 `Items`,筛选出 `item_brand` 不等于 `favorite_brand` 的记录,然后按照 `seller_id` 进行分组,统计每个 `seller_id` 对应的 `item_id` 的个数,最后再使用子查询,找出 `item_id` 个数最多的 `seller_id`。 |
| 108 | + |
| 109 | +<!-- tabs:start --> |
| 110 | + |
| 111 | +### **SQL** |
| 112 | + |
| 113 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 114 | + |
| 115 | +```sql |
| 116 | +# Write your MySQL query statement below |
| 117 | +WITH |
| 118 | + T AS ( |
| 119 | + SELECT seller_id, COUNT(DISTINCT item_id) AS num_items |
| 120 | + FROM |
| 121 | + Orders |
| 122 | + JOIN Users USING (seller_id) |
| 123 | + JOIN Items USING (item_id) |
| 124 | + WHERE item_brand != favorite_brand |
| 125 | + GROUP BY 1 |
| 126 | + ) |
| 127 | +SELECT seller_id, num_items |
| 128 | +FROM T |
| 129 | +WHERE num_items = (SELECT MAX(num_items) FROM T) |
| 130 | +ORDER BY 1; |
| 131 | +``` |
| 132 | + |
| 133 | +<!-- tabs:end --> |
0 commit comments