{ "data": { "question": { "questionId": "3898", "questionFrontendId": "3564", "categoryTitle": "Database", "boundTopicId": 3686016, "title": "Seasonal Sales Analysis", "titleSlug": "seasonal-sales-analysis", "content": "
Table: sales
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| sale_id | int |\n| product_id | int |\n| sale_date | date |\n| quantity | int |\n| price | decimal |\n+---------------+---------+\nsale_id is the unique identifier for this table.\nEach row contains information about a product sale including the product_id, date of sale, quantity sold, and price per unit.\n\n\n
Table: products
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| product_id | int |\n| product_name | varchar |\n| category | varchar |\n+---------------+---------+\nproduct_id is the unique identifier for this table.\nEach row contains information about a product including its name and category.\n\n\n
Write a solution to find the most popular product category for each season. The seasons are defined as:
\n\nThe popularity of a category is determined by the total quantity sold in that season. If there is a tie, select the category with the highest total revenue (quantity × price
).
Return the result table ordered by season in ascending order.
\n\nThe result format is in the following example.
\n\n\n
Example:
\n\nInput:
\n\nsales table:
\n\n\n+---------+------------+------------+----------+-------+\n| sale_id | product_id | sale_date | quantity | price |\n+---------+------------+------------+----------+-------+\n| 1 | 1 | 2023-01-15 | 5 | 10.00 |\n| 2 | 2 | 2023-01-20 | 4 | 15.00 |\n| 3 | 3 | 2023-03-10 | 3 | 18.00 |\n| 4 | 4 | 2023-04-05 | 1 | 20.00 |\n| 5 | 1 | 2023-05-20 | 2 | 10.00 |\n| 6 | 2 | 2023-06-12 | 4 | 15.00 |\n| 7 | 5 | 2023-06-15 | 5 | 12.00 |\n| 8 | 3 | 2023-07-24 | 2 | 18.00 |\n| 9 | 4 | 2023-08-01 | 5 | 20.00 |\n| 10 | 5 | 2023-09-03 | 3 | 12.00 |\n| 11 | 1 | 2023-09-25 | 6 | 10.00 |\n| 12 | 2 | 2023-11-10 | 4 | 15.00 |\n| 13 | 3 | 2023-12-05 | 6 | 18.00 |\n| 14 | 4 | 2023-12-22 | 3 | 20.00 |\n| 15 | 5 | 2024-02-14 | 2 | 12.00 |\n+---------+------------+------------+----------+-------+\n\n\n
products table:
\n\n\n+------------+-----------------+----------+\n| product_id | product_name | category |\n+------------+-----------------+----------+\n| 1 | Warm Jacket | Apparel |\n| 2 | Designer Jeans | Apparel |\n| 3 | Cutting Board | Kitchen |\n| 4 | Smart Speaker | Tech |\n| 5 | Yoga Mat | Fitness |\n+------------+-----------------+----------+\n\n\n
Output:
\n\n\n+---------+----------+----------------+---------------+\n| season | category | total_quantity | total_revenue |\n+---------+----------+----------------+---------------+\n| Fall | Apparel | 10 | 120.00 |\n| Spring | Kitchen | 3 | 54.00 |\n| Summer | Tech | 5 | 100.00 |\n| Winter | Apparel | 9 | 110.00 |\n+---------+----------+----------------+---------------+\n\n\n
Explanation:
\n\nThe result table is ordered by season in ascending order.
\n表:sales
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| sale_id | int |\n| product_id | int |\n| sale_date | date |\n| quantity | int |\n| price | decimal |\n+---------------+---------+\nsale_id 是这张表的唯一主键。\n每一行包含一件产品的销售信息,包括 product_id,销售日期,销售数量,以及单价。\n\n\n
表:products
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| product_id | int |\n| product_name | varchar |\n| category | varchar |\n+---------------+---------+\nproduct_id 是这张表的唯一主键。\n每一行包含一件产品的信息,包括它的名字和分类。\n\n\n
编写一个解决方案来找到每个季节最受欢迎的产品分类。季节定义如下:
\n\n一个 分类 的 受欢迎度 由某个 季节 的 总销售量 决定。如果有并列,选择总收入最高的类别 (quantity × price
)。
返回结果表以季节 升序 排序。
\n\n结果格式如下所示。
\n\n\n\n
示例:
\n\n输入:
\n\nsales 表:
\n\n\n+---------+------------+------------+----------+-------+\n| sale_id | product_id | sale_date | quantity | price |\n+---------+------------+------------+----------+-------+\n| 1 | 1 | 2023-01-15 | 5 | 10.00 |\n| 2 | 2 | 2023-01-20 | 4 | 15.00 |\n| 3 | 3 | 2023-03-10 | 3 | 18.00 |\n| 4 | 4 | 2023-04-05 | 1 | 20.00 |\n| 5 | 1 | 2023-05-20 | 2 | 10.00 |\n| 6 | 2 | 2023-06-12 | 4 | 15.00 |\n| 7 | 5 | 2023-06-15 | 5 | 12.00 |\n| 8 | 3 | 2023-07-24 | 2 | 18.00 |\n| 9 | 4 | 2023-08-01 | 5 | 20.00 |\n| 10 | 5 | 2023-09-03 | 3 | 12.00 |\n| 11 | 1 | 2023-09-25 | 6 | 10.00 |\n| 12 | 2 | 2023-11-10 | 4 | 15.00 |\n| 13 | 3 | 2023-12-05 | 6 | 18.00 |\n| 14 | 4 | 2023-12-22 | 3 | 20.00 |\n| 15 | 5 | 2024-02-14 | 2 | 12.00 |\n+---------+------------+------------+----------+-------+\n\n\n
products 表:
\n\n\n+------------+-----------------+----------+\n| product_id | product_name | category |\n+------------+-----------------+----------+\n| 1 | Warm Jacket | Apparel |\n| 2 | Designer Jeans | Apparel |\n| 3 | Cutting Board | Kitchen |\n| 4 | Smart Speaker | Tech |\n| 5 | Yoga Mat | Fitness |\n+------------+-----------------+----------+\n\n\n
输出:
\n\n\n+---------+----------+----------------+---------------+\n| season | category | total_quantity | total_revenue |\n+---------+----------+----------------+---------------+\n| Fall | Apparel | 10 | 120.00 |\n| Spring | Kitchen | 3 | 54.00 |\n| Summer | Tech | 5 | 100.00 |\n| Winter | Apparel | 9 | 110.00 |\n+---------+----------+----------------+---------------+\n\n\n
解释:
\n\n结果表以季节升序排序。
\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\":{\"sales\":[\"sale_id\",\"product_id\",\"sale_date\",\"quantity\",\"price\"],\"products\":[\"product_id\",\"product_name\",\"category\"]},\"rows\":{\"sales\":[[1,1,\"2023-01-15\",5,10.00],[2,2,\"2023-01-20\",4,15.00],[3,3,\"2023-03-10\",3,18.00],[4,4,\"2023-04-05\",1,20.00],[5,1,\"2023-05-20\",2,10.00],[6,2,\"2023-06-12\",4,15.00],[7,5,\"2023-06-15\",5,12.00],[8,3,\"2023-07-24\",2,18.00],[9,4,\"2023-08-01\",5,20.00],[10,5,\"2023-09-03\",3,12.00],[11,1,\"2023-09-25\",6,10.00],[12,2,\"2023-11-10\",4,15.00],[13,3,\"2023-12-05\",6,18.00],[14,4,\"2023-12-22\",3,20.00],[15,5,\"2024-02-14\",2,12.00]],\"products\":[[1,\"Warm Jacket\",\"Apparel\"],[2,\"Designer Jeans\",\"Apparel\"],[3,\"Cutting Board\",\"Kitchen\"],[4,\"Smart Speaker\",\"Tech\"],[5,\"Yoga Mat\",\"Fitness\"]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"