{ "data": { "question": { "questionId": "1452", "questionFrontendId": "1321", "categoryTitle": "Database", "boundTopicId": 74822, "title": "Restaurant Growth", "titleSlug": "restaurant-growth", "content": "
Table: Customer
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| customer_id | int |\n| name | varchar |\n| visited_on | date |\n| amount | int |\n+---------------+---------+\nIn SQL,(customer_id, visited_on) is the primary key for this table.\nThis table contains data about customer transactions in a restaurant.\nvisited_on is the date on which the customer with ID (customer_id) has visited the restaurant.\namount is the total paid by a customer.\n\n\n
\n\n
You are the restaurant owner and you want to analyze a possible expansion (there will be at least one customer every day).
\n\nCompute the moving average of how much the customer paid in a seven days window (i.e., current day + 6 days before). average_amount
should be rounded to two decimal places.
Return the result table ordered by visited_on
in ascending order.
The result format is in the following example.
\n\n\n
Example 1:
\n\n\nInput: \nCustomer table:\n+-------------+--------------+--------------+-------------+\n| customer_id | name | visited_on | amount |\n+-------------+--------------+--------------+-------------+\n| 1 | Jhon | 2019-01-01 | 100 |\n| 2 | Daniel | 2019-01-02 | 110 |\n| 3 | Jade | 2019-01-03 | 120 |\n| 4 | Khaled | 2019-01-04 | 130 |\n| 5 | Winston | 2019-01-05 | 110 | \n| 6 | Elvis | 2019-01-06 | 140 | \n| 7 | Anna | 2019-01-07 | 150 |\n| 8 | Maria | 2019-01-08 | 80 |\n| 9 | Jaze | 2019-01-09 | 110 | \n| 1 | Jhon | 2019-01-10 | 130 | \n| 3 | Jade | 2019-01-10 | 150 | \n+-------------+--------------+--------------+-------------+\nOutput: \n+--------------+--------------+----------------+\n| visited_on | amount | average_amount |\n+--------------+--------------+----------------+\n| 2019-01-07 | 860 | 122.86 |\n| 2019-01-08 | 840 | 120 |\n| 2019-01-09 | 840 | 120 |\n| 2019-01-10 | 1000 | 142.86 |\n+--------------+--------------+----------------+\nExplanation: \n1st moving average from 2019-01-01 to 2019-01-07 has an average_amount of (100 + 110 + 120 + 130 + 110 + 140 + 150)/7 = 122.86\n2nd moving average from 2019-01-02 to 2019-01-08 has an average_amount of (110 + 120 + 130 + 110 + 140 + 150 + 80)/7 = 120\n3rd moving average from 2019-01-03 to 2019-01-09 has an average_amount of (120 + 130 + 110 + 140 + 150 + 80 + 110)/7 = 120\n4th moving average from 2019-01-04 to 2019-01-10 has an average_amount of (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150)/7 = 142.86\n\n", "translatedTitle": "餐馆营业额变化增长", "translatedContent": "
表: Customer
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| customer_id | int |\n| name | varchar |\n| visited_on | date |\n| amount | int |\n+---------------+---------+\n在 SQL 中,(customer_id, visited_on) 是该表的主键。\n该表包含一家餐馆的顾客交易数据。\nvisited_on 表示 (customer_id) 的顾客在 visited_on 那天访问了餐馆。\namount 是一个顾客某一天的消费总额。\n\n\n
\n\n
你是餐馆的老板,现在你想分析一下可能的营业额变化增长(每天至少有一位顾客)。
\n\n计算以 7 天(某日期 + 该日期前的 6 天)为一个时间段的顾客消费平均值。average_amount
要 保留两位小数。
结果按 visited_on
升序排序。
返回结果格式的例子如下。
\n\n\n\n
示例 1:
\n\n\n输入:\nCustomer 表:\n+-------------+--------------+--------------+-------------+\n| customer_id | name | visited_on | amount |\n+-------------+--------------+--------------+-------------+\n| 1 | Jhon | 2019-01-01 | 100 |\n| 2 | Daniel | 2019-01-02 | 110 |\n| 3 | Jade | 2019-01-03 | 120 |\n| 4 | Khaled | 2019-01-04 | 130 |\n| 5 | Winston | 2019-01-05 | 110 | \n| 6 | Elvis | 2019-01-06 | 140 | \n| 7 | Anna | 2019-01-07 | 150 |\n| 8 | Maria | 2019-01-08 | 80 |\n| 9 | Jaze | 2019-01-09 | 110 | \n| 1 | Jhon | 2019-01-10 | 130 | \n| 3 | Jade | 2019-01-10 | 150 | \n+-------------+--------------+--------------+-------------+\n输出:\n+--------------+--------------+----------------+\n| visited_on | amount | average_amount |\n+--------------+--------------+----------------+\n| 2019-01-07 | 860 | 122.86 |\n| 2019-01-08 | 840 | 120 |\n| 2019-01-09 | 840 | 120 |\n| 2019-01-10 | 1000 | 142.86 |\n+--------------+--------------+----------------+\n解释:\n第一个七天消费平均值从 2019-01-01 到 2019-01-07 是restaurant-growth/restaurant-growth/ (100 + 110 + 120 + 130 + 110 + 140 + 150)/7 = 122.86\n第二个七天消费平均值从 2019-01-02 到 2019-01-08 是 (110 + 120 + 130 + 110 + 140 + 150 + 80)/7 = 120\n第三个七天消费平均值从 2019-01-03 到 2019-01-09 是 (120 + 130 + 110 + 140 + 150 + 80 + 110)/7 = 120\n第四个七天消费平均值从 2019-01-04 到 2019-01-10 是 (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150)/7 = 142.86\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 151, "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 restaurant_growth(customer: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"18.7K\", \"totalSubmission\": \"35.9K\", \"totalAcceptedRaw\": 18742, \"totalSubmissionRaw\": 35855, \"acRate\": \"52.3%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"Customer\":[\"customer_id\",\"name\",\"visited_on\",\"amount\"]},\"rows\":{\"Customer\":[[1,\"Jhon\",\"2019-01-01\",100],[2,\"Daniel\",\"2019-01-02\",110],[3,\"Jade\",\"2019-01-03\",120],[4,\"Khaled\",\"2019-01-04\",130],[5,\"Winston\",\"2019-01-05\",110],[6,\"Elvis\",\"2019-01-06\",140],[7,\"Anna\",\"2019-01-07\",150],[8,\"Maria\",\"2019-01-08\",80],[9,\"Jaze\",\"2019-01-09\",110],[1,\"Jhon\",\"2019-01-10\",130],[3,\"Jade\",\"2019-01-10\",150]]}}", "metaData": "{\"mysql\":[\"Create table If Not Exists Customer (customer_id int, name varchar(20), visited_on date, amount int)\"],\"mssql\":[\"Create table Customer (customer_id int, name varchar(20), visited_on date, amount int)\"],\"oraclesql\":[\"Create table Customer (customer_id int, name varchar(20), visited_on date, amount int)\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"],\"database\":true,\"name\":\"restaurant_growth\",\"pythondata\":[\"Customer = pd.DataFrame([], columns=['customer_id', 'name', 'visited_on', 'amount']).astype({'customer_id':'Int64', 'name':'object', 'visited_on':'datetime64[ns]', 'amount':'Int64'})\"],\"postgresql\":[\"\\nCreate table If Not Exists Customer (customer_id int, name varchar(20), visited_on date, amount int)\"],\"database_schema\":{\"Customer\":{\"customer_id\":\"INT\",\"name\":\"VARCHAR(20)\",\"visited_on\":\"DATE\",\"amount\":\"INT\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Customer (customer_id int, name varchar(20), visited_on date, amount int)", "Truncate table Customer", "insert into Customer (customer_id, name, visited_on, amount) values ('1', 'Jhon', '2019-01-01', '100')", "insert into Customer (customer_id, name, visited_on, amount) values ('2', 'Daniel', '2019-01-02', '110')", "insert into Customer (customer_id, name, visited_on, amount) values ('3', 'Jade', '2019-01-03', '120')", "insert into Customer (customer_id, name, visited_on, amount) values ('4', 'Khaled', '2019-01-04', '130')", "insert into Customer (customer_id, name, visited_on, amount) values ('5', 'Winston', '2019-01-05', '110')", "insert into Customer (customer_id, name, visited_on, amount) values ('6', 'Elvis', '2019-01-06', '140')", "insert into Customer (customer_id, name, visited_on, amount) values ('7', 'Anna', '2019-01-07', '150')", "insert into Customer (customer_id, name, visited_on, amount) values ('8', 'Maria', '2019-01-08', '80')", "insert into Customer (customer_id, name, visited_on, amount) values ('9', 'Jaze', '2019-01-09', '110')", "insert into Customer (customer_id, name, visited_on, amount) values ('1', 'Jhon', '2019-01-10', '130')", "insert into Customer (customer_id, name, visited_on, amount) values ('3', 'Jade', '2019-01-10', '150')" ], "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.0.2 and NumPy 1.25.0<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\" PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"Customer\":[\"customer_id\",\"name\",\"visited_on\",\"amount\"]},\"rows\":{\"Customer\":[[1,\"Jhon\",\"2019-01-01\",100],[2,\"Daniel\",\"2019-01-02\",110],[3,\"Jade\",\"2019-01-03\",120],[4,\"Khaled\",\"2019-01-04\",130],[5,\"Winston\",\"2019-01-05\",110],[6,\"Elvis\",\"2019-01-06\",140],[7,\"Anna\",\"2019-01-07\",150],[8,\"Maria\",\"2019-01-08\",80],[9,\"Jaze\",\"2019-01-09\",110],[1,\"Jhon\",\"2019-01-10\",130],[3,\"Jade\",\"2019-01-10\",150]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"