2023-12-09 18:53:53 +08:00
{
"data" : {
"question" : {
"questionId" : "1390" ,
"questionFrontendId" : "1251" ,
"categoryTitle" : "Database" ,
"boundTopicId" : 41811 ,
"title" : "Average Selling Price" ,
"titleSlug" : "average-selling-price" ,
"content" : "<p>Table: <code>Prices</code></p>\n\n<pre>\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| product_id | int |\n| start_date | date |\n| end_date | date |\n| price | int |\n+---------------+---------+\n(product_id, start_date, end_date) is the primary key (combination of columns with unique values) for this table.\nEach row of this table indicates the price of the product_id in the period from start_date to end_date.\nFor each product_id there will be no two overlapping periods. That means there will be no two intersecting periods for the same product_id.\n</pre>\n\n<p> </p>\n\n<p>Table: <code>UnitsSold</code></p>\n\n<pre>\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| product_id | int |\n| purchase_date | date |\n| units | int |\n+---------------+---------+\nThis table may contain duplicate rows.\nEach row of this table indicates the date, units, and product_id of each product sold. \n</pre>\n\n<p> </p>\n\n<p>Write a solution to find the average selling price for each product. <code>average_price</code> should be <strong>rounded to 2 decimal places</strong>.</p>\n\n<p>Return the result table in <strong>any order</strong>.</p>\n\n<p>The result format is in the following example.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nPrices table:\n+------------+------------+------------+--------+\n| product_id | start_date | end_date | price |\n+------------+------------+------------+--------+\n| 1 | 2019-02-17 | 2019-02-28 | 5 |\n| 1 | 2019-03-01 | 2019-03-22 | 20 |\n| 2 | 2019-02-01 | 2019-02-20 | 15 |\n| 2 | 2019-02-21 | 2019-03-31 | 30 |\n+------------+------------+------------+--------+\nUnitsSold table:\n+------------+---------------+-------+\n| product_id | purchase_date | units |\n+------------+---------------+-------+\n| 1 | 2019-02-25 | 100 |\n| 1 | 2019-03-01 | 15 |\n| 2 | 2019-02-10 | 200 |\n| 2 | 2019-03-22 | 30 |\n+------------+---------------+-------+\n<strong>Output:</strong> \n+------------+---------------+\n| product_id | average_price |\n+------------+---------------+\n| 1 | 6.96 |\n| 2 | 16.96 |\n+------------+---------------+\n<strong>Explanation:</strong> \nAverage selling price = Total Price of Product / Number of products sold.\nAverage selling price for product 1 = ((100 * 5) + (15 * 20)) / 115 = 6.96\nAverage selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96\n</pre>\n" ,
"translatedTitle" : "平均售价" ,
"translatedContent" : "<p>表:<code>Prices</code></p>\n\n<pre>\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| product_id | int |\n| start_date | date |\n| end_date | date |\n| price | int |\n+---------------+---------+\n(product_id, start_date, end_date) 是 <code>prices</code> 表的主键(具有唯一值的列的组合)。\n<code>prices</code> 表的每一行表示的是某个产品在一段时期内的价格。\n每个产品的对应时间段是不会重叠的, 这也意味着同一个产品的价格时段不会出现交叉。</pre>\n\n<p> </p>\n\n<p>表:<code>UnitsSold</code></p>\n\n<pre>\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| product_id | int |\n| purchase_date | date |\n| units | int |\n+---------------+---------+\n<span style=\"white-space: pre-wrap;\">该表可能包含重复数据</span>。\n<span style=\"white-space: pre-wrap;\">该</span>表的每一行表示的是每种产品的出售日期,单位和产品 id。</pre>\n\n<p> </p>\n\n<p>编写解决方案以查找每种产品的平均售价。<code>average_price</code> 应该 <strong>四舍五入到小数点后两位</strong>。</p>\n\n<p>返回结果表 <strong>无顺序要求</strong> 。</p>\n\n<p>结果格式如下例所示。</p>\n\n<p> </p>\n\n<p><strong>示例 1: </strong></p>\n\n<pre>\n<strong>输入:</strong>\nPrices table:\n+------------+------------+------------+--------+\n| product_id | start_date | end_date | price |\n+------------+------------+------------+--------+\n| 1 | 2019-02-17 | 2019-02-28 | 5 |\n| 1 | 2019-03-01 | 2019-03-22 | 20 |\n| 2 | 2019-02-01 | 2019-02-20 | 15 |\n| 2 | 2019-02-21 | 2019-03-31 | 30 |\n+------------+------------+------------+--------+\nUnitsSold table:\n+------------+---------------+-------+\n| product_id | purchase_date | units |\n+------------+---------------+-------+\n| 1 | 2019-02-25 | 100 |\n| 1 | 2019-03-01 | 15 |\n| 2 | 2019-02-10 | 200 |\n| 2 | 2019-03-22 | 30 |\n+------------+---------------+-------+\n<strong>输出:</strong>\n+------------+---------------+\n| product_id | average_price |\n+------------+---------------+\n| 1 | 6.96 |\n| 2 | 16.96 |\n+------------+---------------+\n<strong>解释:</strong>\n平均售价 = 产品总价 / 销售的产品数量。\n产品 1 的平均售价 = ((100 * 5)+(15 * 20) )/ 115 = 6.96\n产品 2 的平均售价 = ((200 * 15)+(30 * 30) )/ 230 = 16.96</pre>\n" ,
"isPaidOnly" : false ,
"difficulty" : "Easy" ,
"likes" : 110 ,
"dislikes" : 0 ,
"isLiked" : null ,
"similarQuestions" : "[]" ,
"contributors" : [ ] ,
"langToValidPlayground" : "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": 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" : "Oracle" ,
"langSlug" : "oraclesql" ,
"code" : "/* Write your PL/SQL query statement below */" ,
"__typename" : "CodeSnippetNode"
} ,
{
"lang" : "Pandas" ,
"langSlug" : "pythondata" ,
"code" : "import pandas as pd\n\ndef average_selling_price(prices: pd.DataFrame, units_sold: pd.DataFrame) -> pd.DataFrame:\n " ,
"__typename" : "CodeSnippetNode"
} ,
{
"lang" : "PostgreSQL" ,
"langSlug" : "postgresql" ,
"code" : "-- Write your PostgreSQL query statement below" ,
"__typename" : "CodeSnippetNode"
}
] ,
2023-12-09 19:57:46 +08:00
"stats" : "{\"totalAccepted\": \"36.8K\", \"totalSubmission\": \"64.6K\", \"totalAcceptedRaw\": 36788, \"totalSubmissionRaw\": 64602, \"acRate\": \"56.9%\"}" ,
2023-12-09 18:53:53 +08:00
"hints" : [ ] ,
"solution" : null ,
"status" : null ,
"sampleTestCase" : "{\"headers\":{\"Prices\":[\"product_id\",\"start_date\",\"end_date\",\"price\"],\"UnitsSold\":[\"product_id\",\"purchase_date\",\"units\"]},\"rows\":{\"Prices\":[[1,\"2019-02-17\",\"2019-02-28\",5],[1,\"2019-03-01\",\"2019-03-22\",20],[2,\"2019-02-01\",\"2019-02-20\",15],[2,\"2019-02-21\",\"2019-03-31\",30]],\"UnitsSold\":[[1,\"2019-02-25\",100],[1,\"2019-03-01\",15],[2,\"2019-02-10\",200],[2,\"2019-03-22\",30]]}}" ,
"metaData" : "{\"mysql\":[\"Create table If Not Exists Prices (product_id int, start_date date, end_date date, price int)\",\"Create table If Not Exists UnitsSold (product_id int, purchase_date date, units int)\"],\"mssql\":[\"Create table Prices (product_id int, start_date date, end_date date, price int)\",\"Create table UnitsSold (product_id int, purchase_date date, units int)\"],\"oraclesql\":[\"Create table Prices (product_id int, start_date date, end_date date, price int)\",\"Create table UnitsSold (product_id int, purchase_date date, units int)\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"],\"database\":true,\"name\":\"average_selling_price\",\"pythondata\":[\"Prices = pd.DataFrame([], columns=['product_id', 'start_date', 'end_date', 'price']).astype({'product_id':'Int64', 'start_date':'datetime64[ns]', 'end_date':'datetime64[ns]', 'price':'Int64'})\",\"UnitsSold = pd.DataFrame([], columns=['product_id', 'purchase_date', 'units']).astype({'product_id':'Int64', 'purchase_date':'datetime64[ns]', 'units':'Int64'})\"],\"postgresql\":[\"\\nCreate table If Not Exists Prices (product_id int, start_date date, end_date date, price int)\\n\",\"Create table If Not Exists UnitsSold (product_id int, purchase_date date, units int)\"],\"database_schema\":{\"Prices\":{\"product_id\":\"INT\",\"start_date\":\"DATE\",\"end_date\":\"DATE\",\"price\":\"INT\"},\"UnitsSold\":{\"product_id\":\"INT\",\"purchase_date\":\"DATE\",\"units\":\"INT\"}}}" ,
"judgerAvailable" : true ,
"judgeType" : "large" ,
"mysqlSchemas" : [
"Create table If Not Exists Prices (product_id int, start_date date, end_date date, price int)" ,
"Create table If Not Exists UnitsSold (product_id int, purchase_date date, units int)" ,
"Truncate table Prices" ,
"insert into Prices (product_id, start_date, end_date, price) values ('1', '2019-02-17', '2019-02-28', '5')" ,
"insert into Prices (product_id, start_date, end_date, price) values ('1', '2019-03-01', '2019-03-22', '20')" ,
"insert into Prices (product_id, start_date, end_date, price) values ('2', '2019-02-01', '2019-02-20', '15')" ,
"insert into Prices (product_id, start_date, end_date, price) values ('2', '2019-02-21', '2019-03-31', '30')" ,
"Truncate table UnitsSold" ,
"insert into UnitsSold (product_id, purchase_date, units) values ('1', '2019-02-25', '100')" ,
"insert into UnitsSold (product_id, purchase_date, units) values ('1', '2019-03-01', '15')" ,
"insert into UnitsSold (product_id, purchase_date, units) values ('2', '2019-02-10', '200')" ,
"insert into UnitsSold (product_id, purchase_date, units) values ('2', '2019-03-22', '30')"
] ,
"enableRunCode" : true ,
"envInfo" : "{\"mysql\":[\"MySQL\",\"<p>\\u7248\\u672c\\uff1a<code>MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"<p>mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"<p>Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\"<p>Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\"<p>PostgreSQL 16<\\/p>\"]}" ,
"book" : null ,
"isSubscribed" : false ,
"isDailyQuestion" : false ,
"dailyRecordStatus" : null ,
"editorType" : "CKEDITOR" ,
"ugcQuestionId" : null ,
"style" : "LEETCODE" ,
"exampleTestcases" : "{\"headers\":{\"Prices\":[\"product_id\",\"start_date\",\"end_date\",\"price\"],\"UnitsSold\":[\"product_id\",\"purchase_date\",\"units\"]},\"rows\":{\"Prices\":[[1,\"2019-02-17\",\"2019-02-28\",5],[1,\"2019-03-01\",\"2019-03-22\",20],[2,\"2019-02-01\",\"2019-02-20\",15],[2,\"2019-02-21\",\"2019-03-31\",30]],\"UnitsSold\":[[1,\"2019-02-25\",100],[1,\"2019-03-01\",15],[2,\"2019-02-10\",200],[2,\"2019-03-22\",30]]}}" ,
"__typename" : "QuestionNode"
}
}
}