{ "data": { "question": { "questionId": "1155", "questionFrontendId": "1070", "categoryTitle": "Database", "boundTopicId": 7635, "title": "Product Sales Analysis III", "titleSlug": "product-sales-analysis-iii", "content": "
Table: Sales
\n+-------------+-------+\n| Column Name | Type |\n+-------------+-------+\n| sale_id | int |\n| product_id | int |\n| year | int |\n| quantity | int |\n| price | int |\n+-------------+-------+\n(sale_id, year) is the primary key (combination of columns with unique values) of this table.\nproduct_id is a foreign key (reference column) to Product
table.\nEach row records a sale of a product in a given year.\nA product may have multiple sales entries in the same year.\nNote that the per-unit price.\n\n
\n\nWrite a solution to find all sales that occurred in the first year each product was sold.
\n\nFor each product_id
, identify the earliest year
it appears in the Sales
table.
Return all sales entries for that product in that year.
\n\tReturn a table with the following columns: product_id, first_year, quantity, and price.
\nReturn the result in any order.
\n
Example 1:
\n\n\nInput: \nSales table:\n+---------+------------+------+----------+-------+\n| sale_id | product_id | year | quantity | price |\n+---------+------------+------+----------+-------+ \n| 1 | 100 | 2008 | 10 | 5000 |\n| 2 | 100 | 2009 | 12 | 5000 |\n| 7 | 200 | 2011 | 15 | 9000 |\n+---------+------------+------+----------+-------+\n\nOutput: \n+------------+------------+----------+-------+\n| product_id | first_year | quantity | price |\n+------------+------------+----------+-------+ \n| 100 | 2008 | 10 | 5000 |\n| 200 | 2011 | 15 | 9000 |\n+------------+------------+----------+-------+\n\n", "translatedTitle": "产品销售分析 III", "translatedContent": "
销售表 Sales
:
\n+-------------+-------+\n| Column Name | Type |\n+-------------+-------+\n| sale_id | int |\n| product_id | int |\n| year | int |\n| quantity | int |\n| price | int |\n+-------------+-------+\n(sale_id, year) 是这张表的主键(具有唯一值的列的组合)。\nproduct_id 是产品表的外键(reference 列)。\n这张表的每一行都表示:编号 product_id 的产品在某一年的销售额。\n一个产品可能在同一年内有多个销售条目。\n请注意,价格是按每单位计的。\n\n\n
编写解决方案,选出每个售出过的产品 第一年 销售的 产品 id、年份、数量 和 价格。
\n\nproduct_id
,找到其在Sales表中首次出现的最早年份。返回一张有这些列的表:product_id,first_year,quantity 和 price。
\n\n结果表中的条目可以按 任意顺序 排列。
\n\n\n\n
示例 1:
\n\n\n输入:\nSales 表:\n+---------+------------+------+----------+-------+\n| sale_id | product_id | year | quantity | price |\n+---------+------------+------+----------+-------+ \n| 1 | 100 | 2008 | 10 | 5000 |\n| 2 | 100 | 2009 | 12 | 5000 |\n| 7 | 200 | 2011 | 15 | 9000 |\n+---------+------------+------+----------+-------+\n输出:\n+------------+------------+----------+-------+\n| product_id | first_year | quantity | price |\n+------------+------------+----------+-------+ \n| 100 | 2008 | 10 | 5000 |\n| 200 | 2011 | 15 | 9000 |\n+------------+------------+----------+-------+\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 59, "dislikes": 0, "isLiked": null, "similarQuestions": "[{\"title\": \"Product Sales Analysis II\", \"titleSlug\": \"product-sales-analysis-ii\", \"difficulty\": \"Easy\", \"translatedTitle\": \"\\u4ea7\\u54c1\\u9500\\u552e\\u5206\\u6790 II\", \"isPaidOnly\": true}, {\"title\": \"Product Sales Analysis IV\", \"titleSlug\": \"product-sales-analysis-iv\", \"difficulty\": \"Medium\", \"translatedTitle\": \"\\u4ea7\\u54c1\\u9500\\u552e\\u5206\\u6790 IV\", \"isPaidOnly\": true}, {\"title\": \"Product Sales Analysis V\", \"titleSlug\": \"product-sales-analysis-v\", \"difficulty\": \"Easy\", \"translatedTitle\": \"\\u4ea7\\u54c1\\u9500\\u552e\\u5206\\u6790\\u2164\", \"isPaidOnly\": true}]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python3\": false, \"python\": false, \"javascript\": false, \"typescript\": false, \"csharp\": false, \"c\": false, \"golang\": false, \"kotlin\": false, \"swift\": false, \"rust\": false, \"ruby\": false, \"php\": false, \"dart\": false, \"scala\": false, \"elixir\": false, \"erlang\": false, \"racket\": false, \"cangjie\": false, \"bash\": false, \"html\": false, \"pythonml\": false, \"react\": false, \"vanillajs\": false, \"mysql\": false, \"mssql\": false, \"postgresql\": false, \"oraclesql\": false, \"pythondata\": false}", "topicTags": [ { "name": "Database", "slug": "database", "translatedName": "数据库", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef sales_analysis(sales: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"35.8K\", \"totalSubmission\": \"76.5K\", \"totalAcceptedRaw\": 35790, \"totalSubmissionRaw\": 76477, \"acRate\": \"46.8%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"Sales\":[\"sale_id\",\"product_id\",\"year\",\"quantity\",\"price\"]},\"rows\":{\"Sales\":[[1,100,2008,10,5000],[2,100,2009,12,5000],[7,200,2011,15,9000]]}}", "metaData": "{\"mysql\":[\"Create table If Not Exists Sales (sale_id int, product_id int, year int, quantity int, price int)\"],\"mssql\":[\"Create table Sales (sale_id int, product_id int, year int, quantity int, price int)\"],\"oraclesql\":[\"Create table Sales (sale_id int, product_id int, year int, quantity int, price int)\"],\"database\":true,\"name\":\"sales_analysis\",\"pythondata\":[\"Sales = pd.DataFrame([], columns=['sale_id', 'product_id', 'year', 'quantity', 'price']).astype({'sale_id':'Int64', 'product_id':'Int64', 'year':'Int64', 'quantity':'Int64', 'price':'Int64'})\"],\"postgresql\":[\"Create table If Not Exists Sales (sale_id int, product_id int, year int, quantity int, price int)\\n\"],\"database_schema\":{\"Sales\":{\"sale_id\":\"INT\",\"product_id\":\"INT\",\"year\":\"INT\",\"quantity\":\"INT\",\"price\":\"INT\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Sales (sale_id int, product_id int, year int, quantity int, price int)", "Truncate table Sales", "insert into Sales (sale_id, product_id, year, quantity, price) values ('1', '100', '2008', '10', '5000')", "insert into Sales (sale_id, product_id, year, quantity, price) values ('2', '100', '2009', '12', '5000')", "insert into Sales (sale_id, product_id, year, quantity, price) values ('7', '200', '2011', '15', '9000')" ], "enableRunCode": true, "envInfo": "{\"mysql\":[\"MySQL\",\"
\\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\",\"year\",\"quantity\",\"price\"]},\"rows\":{\"Sales\":[[1,100,2008,10,5000],[2,100,2009,12,5000],[7,200,2011,15,9000]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"