{ "data": { "question": { "questionId": "3891", "questionFrontendId": "3554", "categoryTitle": "Database", "boundTopicId": 3680766, "title": "Find Category Recommendation Pairs", "titleSlug": "find-category-recommendation-pairs", "content": "
Table: ProductPurchases
\n+-------------+------+\n| Column Name | Type | \n+-------------+------+\n| user_id | int |\n| product_id | int |\n| quantity | int |\n+-------------+------+\n(user_id, product_id) is the unique identifier for this table. \nEach row represents a purchase of a product by a user in a specific quantity.\n\n\n
Table: ProductInfo
\n+-------------+---------+\n| Column Name | Type | \n+-------------+---------+\n| product_id | int |\n| category | varchar |\n| price | decimal |\n+-------------+---------+\nproduct_id is the unique identifier for this table.\nEach row assigns a category and price to a product.\n\n\n
Amazon wants to understand shopping patterns across product categories. Write a solution to:
\n\ncategory1
< category2
)A category pair is considered reportable if at least 3
different customers have purchased products from both categories.
Return the result table of reportable category pairs ordered by customer_count in descending order, and in case of a tie, by category1 in ascending order lexicographically, and then by category2 in ascending order.
\n\nThe result format is in the following example.
\n\n\n
Example:
\n\nInput:
\n\nProductPurchases table:
\n\n\n+---------+------------+----------+\n| user_id | product_id | quantity |\n+---------+------------+----------+\n| 1 | 101 | 2 |\n| 1 | 102 | 1 |\n| 1 | 201 | 3 |\n| 1 | 301 | 1 |\n| 2 | 101 | 1 |\n| 2 | 102 | 2 |\n| 2 | 103 | 1 |\n| 2 | 201 | 5 |\n| 3 | 101 | 2 |\n| 3 | 103 | 1 |\n| 3 | 301 | 4 |\n| 3 | 401 | 2 |\n| 4 | 101 | 1 |\n| 4 | 201 | 3 |\n| 4 | 301 | 1 |\n| 4 | 401 | 2 |\n| 5 | 102 | 2 |\n| 5 | 103 | 1 |\n| 5 | 201 | 2 |\n| 5 | 202 | 3 |\n+---------+------------+----------+\n\n\n
ProductInfo table:
\n\n\n+------------+-------------+-------+\n| product_id | category | price |\n+------------+-------------+-------+\n| 101 | Electronics | 100 |\n| 102 | Books | 20 |\n| 103 | Books | 35 |\n| 201 | Clothing | 45 |\n| 202 | Clothing | 60 |\n| 301 | Sports | 75 |\n| 401 | Kitchen | 50 |\n+------------+-------------+-------+\n\n\n
Output:
\n\n\n+-------------+-------------+----------------+\n| category1 | category2 | customer_count |\n+-------------+-------------+----------------+\n| Books | Clothing | 3 |\n| Books | Electronics | 3 |\n| Clothing | Electronics | 3 |\n| Electronics | Sports | 3 |\n+-------------+-------------+----------------+\n\n\n
Explanation:
\n\nThe result is ordered by customer_count in descending order. Since all pairs have the same customer_count of 3, they are ordered by category1 (then category2) in ascending order.
\n表:ProductPurchases
\n+-------------+------+\n| Column Name | Type | \n+-------------+------+\n| user_id | int |\n| product_id | int |\n| quantity | int |\n+-------------+------+\n(user_id, product_id) 是这张表的唯一主键。\n每一行代表用户以特定数量购买的一种产品。\n\n\n
表:ProductInfo
\n+-------------+---------+\n| Column Name | Type | \n+-------------+---------+\n| product_id | int |\n| category | varchar |\n| price | decimal |\n+-------------+---------+\nproduct_id 是这张表的唯一主键。\n每一行表示一件商品的类别和价格。\n\n\n
亚马逊想要了解不同产品类别的购物模式。编写一个解决方案:
\n\ncategory1
< category2
)如果至少有 3
个不同的客户购买了两个类别的产品,则类别对被视为 可报告的。
返回可报告类别对的结果表以 customer_count 降序 排序,并且为了防止排序持平,以 category1 字典序 升序 排序,然后以 category2 升序 排序。
\n\n结果格式如下所示。
\n\n\n\n
示例:
\n\n输入:
\n\nProductPurchases 表:
\n\n\n+---------+------------+----------+\n| user_id | product_id | quantity |\n+---------+------------+----------+\n| 1 | 101 | 2 |\n| 1 | 102 | 1 |\n| 1 | 201 | 3 |\n| 1 | 301 | 1 |\n| 2 | 101 | 1 |\n| 2 | 102 | 2 |\n| 2 | 103 | 1 |\n| 2 | 201 | 5 |\n| 3 | 101 | 2 |\n| 3 | 103 | 1 |\n| 3 | 301 | 4 |\n| 3 | 401 | 2 |\n| 4 | 101 | 1 |\n| 4 | 201 | 3 |\n| 4 | 301 | 1 |\n| 4 | 401 | 2 |\n| 5 | 102 | 2 |\n| 5 | 103 | 1 |\n| 5 | 201 | 2 |\n| 5 | 202 | 3 |\n+---------+------------+----------+\n\n\n
ProductInfo 表:
\n\n\n+------------+-------------+-------+\n| product_id | category | price |\n+------------+-------------+-------+\n| 101 | Electronics | 100 |\n| 102 | Books | 20 |\n| 103 | Books | 35 |\n| 201 | Clothing | 45 |\n| 202 | Clothing | 60 |\n| 301 | Sports | 75 |\n| 401 | Kitchen | 50 |\n+------------+-------------+-------+\n\n\n
输出:
\n\n\n+-------------+-------------+----------------+\n| category1 | category2 | customer_count |\n+-------------+-------------+----------------+\n| Books | Clothing | 3 |\n| Books | Electronics | 3 |\n| Clothing | Electronics | 3 |\n| Electronics | Sports | 3 |\n+-------------+-------------+----------------+\n\n\n
解释:
\n\n结果按 customer_count 降序排列。由于所有对都有相同的客户数量 3,它们按 category1(然后是 category2)升序排列。
\n\\u7248\\u672c\\uff1a mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\" Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\" Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\" PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"ProductPurchases\":[\"user_id\",\"product_id\",\"quantity\"],\"ProductInfo\":[\"product_id\",\"category\",\"price\"]},\"rows\":{\"ProductPurchases\":[[1,101,2],[1,102,1],[1,201,3],[1,301,1],[2,101,1],[2,102,2],[2,103,1],[2,201,5],[3,101,2],[3,103,1],[3,301,4],[3,401,2],[4,101,1],[4,201,3],[4,301,1],[4,401,2],[5,102,2],[5,103,1],[5,201,2],[5,202,3]],\"ProductInfo\":[[101,\"Electronics\",100],[102,\"Books\",20],[103,\"Books\",35],[201,\"Clothing\",45],[202,\"Clothing\",60],[301,\"Sports\",75],[401,\"Kitchen\",50]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"