{ "data": { "question": { "questionId": "1452", "questionFrontendId": "1321", "boundTopicId": null, "title": "Restaurant Growth", "titleSlug": "restaurant-growth", "content": "

Table: Customer

\n\n
\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\n

Compute 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.

\n\n

Return the result table ordered by visited_on in ascending order.

\n\n

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": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 630, "dislikes": 225, "isLiked": null, "similarQuestions": "[]", "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]]}}", "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 restaurant_growth(customer: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below\n", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"54.9K\", \"totalSubmission\": \"114.2K\", \"totalAcceptedRaw\": 54876, \"totalSubmissionRaw\": 114248, \"acRate\": \"48.0%\"}", "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, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"mysql\": [\"MySQL\", \"

MySQL 8.0.

\"], \"mssql\": [\"MS SQL Server\", \"

mssql server 2019.

\"], \"oraclesql\": [\"Oracle\", \"

Oracle Sql 11.2.

\"], \"pythondata\": [\"Pandas\", \"

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" } } }