{ "data": { "question": { "questionId": "1155", "questionFrontendId": "1070", "boundTopicId": null, "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 of this table shows a sale on the product product_id in a certain year.\nNote that the price is per unit.\n
\n\n\n\n
Table: Product
\n+--------------+---------+\n| Column Name | Type |\n+--------------+---------+\n| product_id | int |\n| product_name | varchar |\n+--------------+---------+\nproduct_id is the primary key (column with unique values) of this table.\nEach row of this table indicates the product name of each product.\n\n\n
\n\n
Write a solution to select the product id, year, quantity, and price for the first year of every product sold.
\n\nReturn the resulting table in any order.
\n\nThe result format is in the following example.
\n\n\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+---------+------------+------+----------+-------+\nProduct table:\n+------------+--------------+\n| product_id | product_name |\n+------------+--------------+\n| 100 | Nokia |\n| 200 | Apple |\n| 300 | Samsung |\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": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 292, "dislikes": 737, "isLiked": null, "similarQuestions": "[{\"title\": \"Product Sales Analysis II\", \"titleSlug\": \"product-sales-analysis-ii\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Product Sales Analysis IV\", \"titleSlug\": \"product-sales-analysis-iv\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Product Sales Analysis V\", \"titleSlug\": \"product-sales-analysis-v\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "exampleTestcases": "{\"headers\":{\"Sales\":[\"sale_id\",\"product_id\",\"year\",\"quantity\",\"price\"],\"Product\":[\"product_id\",\"product_name\"]},\"rows\":{\"Sales\":[[1,100,2008,10,5000],[2,100,2009,12,5000],[7,200,2011,15,9000]],\"Product\":[[100,\"Nokia\"],[200,\"Apple\"],[300,\"Samsung\"]]}}", "categoryTitle": "Database", "contributors": [], "topicTags": [ { "name": "Database", "slug": "database", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below\n", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */\n", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */\n", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef sales_analysis(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below\n", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"87K\", \"totalSubmission\": \"202.8K\", \"totalAcceptedRaw\": 86956, \"totalSubmissionRaw\": 202782, \"acRate\": \"42.9%\"}", "hints": [], "solution": { "id": "2133", "canSeeDetail": true, "paidOnly": false, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "status": null, "sampleTestCase": "{\"headers\":{\"Sales\":[\"sale_id\",\"product_id\",\"year\",\"quantity\",\"price\"],\"Product\":[\"product_id\",\"product_name\"]},\"rows\":{\"Sales\":[[1,100,2008,10,5000],[2,100,2009,12,5000],[7,200,2011,15,9000]],\"Product\":[[100,\"Nokia\"],[200,\"Apple\"],[300,\"Samsung\"]]}}", "metaData": "{\"mysql\": [\"Create table If Not Exists Sales (sale_id int, product_id int, year int, quantity int, price int)\", \"Create table If Not Exists Product (product_id int, product_name varchar(10))\"], \"mssql\": [\"Create table Sales (sale_id int, product_id int, year int, quantity int, price int)\", \"Create table Product (product_id int, product_name varchar(10))\"], \"oraclesql\": [\"Create table Sales (sale_id int, product_id int, year int, quantity int, price int)\", \"Create table Product (product_id int, product_name varchar(10))\"], \"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'})\", \"Product = pd.DataFrame([], columns=['product_id', 'product_name']).astype({'product_id':'Int64', 'product_name':'object'})\"], \"postgresql\": [\"Create table If Not Exists Sales (sale_id int, product_id int, year int, quantity int, price int)\\n\", \"Create table If Not Exists Product (product_id int, product_name varchar(10))\"], \"database_schema\": {\"Sales\": {\"sale_id\": \"INT\", \"product_id\": \"INT\", \"year\": \"INT\", \"quantity\": \"INT\", \"price\": \"INT\"}, \"Product\": {\"product_id\": \"INT\", \"product_name\": \"VARCHAR(10)\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Sales (sale_id int, product_id int, year int, quantity int, price int)", "Create table If Not Exists Product (product_id int, product_name varchar(10))", "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')", "Truncate table Product", "insert into Product (product_id, product_name) values ('100', 'Nokia')", "insert into Product (product_id, product_name) values ('200', 'Apple')", "insert into Product (product_id, product_name) values ('300', 'Samsung')" ], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"mysql\": [\"MySQL\", \"
MySQL 8.0
.
mssql server 2019
.
Oracle Sql 11.2
.
Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0
\"], \"postgresql\": [\"PostgreSQL\", \"PostgreSQL 16
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }