{ "data": { "question": { "questionId": "3865", "questionFrontendId": "3521", "categoryTitle": "Database", "boundTopicId": 3653631, "title": "Find Product Recommendation Pairs", "titleSlug": "find-product-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 key 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 primary key for this table.\nEach row assigns a category and price to a product.\n\n\n
Amazon wants to implement the Customers who bought this also bought... feature based on co-purchase patterns. Write a solution to :
\n\nproduct1_id
< product2_id
)A product pair is considered for recommendation if at least 3
different customers have purchased both products.
Return the result table ordered by customer_count in descending order, and in case of a tie, by product1_id
in ascending order, and then by product2_id
in ascending order.
The 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 | 103 | 3 |\n| 2 | 101 | 1 |\n| 2 | 102 | 5 |\n| 2 | 104 | 1 |\n| 3 | 101 | 2 |\n| 3 | 103 | 1 |\n| 3 | 105 | 4 |\n| 4 | 101 | 1 |\n| 4 | 102 | 1 |\n| 4 | 103 | 2 |\n| 4 | 104 | 3 |\n| 5 | 102 | 2 |\n| 5 | 104 | 1 |\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 | Clothing | 35 |\n| 104 | Kitchen | 50 |\n| 105 | Sports | 75 |\n+------------+-------------+-------+\n\n\n
Output:
\n\n\n+-------------+-------------+-------------------+-------------------+----------------+\n| product1_id | product2_id | product1_category | product2_category | customer_count |\n+-------------+-------------+-------------------+-------------------+----------------+\n| 101 | 102 | Electronics | Books | 3 |\n| 101 | 103 | Electronics | Clothing | 3 |\n| 102 | 104 | Books | Kitchen | 3 |\n+-------------+-------------+-------------------+-------------------+----------------+\n\n\n
Explanation:
\n\nThe result is ordered by customer_count in descending order. For pairs with the same customer_count, they are ordered by product1_id and then product2_id 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\nproduct1_id
< product2_id
)如果 至少有 3
位不同的 客户同时购买了这两种产品,则认为该 产品对 适合推荐。
返回结果表以 customer_count 降序 排序,并且为了避免排序持平,以 product1_id
升序 排序,并以 product2_id
升序 排序。
结果格式如下所示。
\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 | 103 | 3 |\n| 2 | 101 | 1 |\n| 2 | 102 | 5 |\n| 2 | 104 | 1 |\n| 3 | 101 | 2 |\n| 3 | 103 | 1 |\n| 3 | 105 | 4 |\n| 4 | 101 | 1 |\n| 4 | 102 | 1 |\n| 4 | 103 | 2 |\n| 4 | 104 | 3 |\n| 5 | 102 | 2 |\n| 5 | 104 | 1 |\n+---------+------------+----------+\n\n\n
ProductInfo 表:
\n\n\n+------------+-------------+-------+\n| product_id | category | price |\n+------------+-------------+-------+\n| 101 | Electronics | 100 |\n| 102 | Books | 20 |\n| 103 | Clothing | 35 |\n| 104 | Kitchen | 50 |\n| 105 | Sports | 75 |\n+------------+-------------+-------+\n\n\n
输出:
\n\n\n+-------------+-------------+-------------------+-------------------+----------------+\n| product1_id | product2_id | product1_category | product2_category | customer_count |\n+-------------+-------------+-------------------+-------------------+----------------+\n| 101 | 102 | Electronics | Books | 3 |\n| 101 | 103 | Electronics | Clothing | 3 |\n| 102 | 104 | Books | Kitchen | 3 |\n+-------------+-------------+-------------------+-------------------+----------------+\n\n\n
解释:
\n\n结果以 customer_count 降序排序。对于有相同 customer_count 的产品对,将它们以 product1_id 升序排序,然后以 product2_id 升序排序。
\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,103,3],[2,101,1],[2,102,5],[2,104,1],[3,101,2],[3,103,1],[3,105,4],[4,101,1],[4,102,1],[4,103,2],[4,104,3],[5,102,2],[5,104,1]],\"ProductInfo\":[[101,\"Electronics\",100],[102,\"Books\",20],[103,\"Clothing\",35],[104,\"Kitchen\",50],[105,\"Sports\",75]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"