# Write your MySQL query statement below
1321. 餐馆营业额变化增长
表: Customer
+---------------+---------+ | Column Name | Type | +---------------+---------+ | customer_id | int | | name | varchar | | visited_on | date | | amount | int | +---------------+---------+ (customer_id, visited_on) 是该表的主键。 该表包含一家餐馆的顾客交易数据。 visited_on 表示 (customer_id) 的顾客在 visited_on 那天访问了餐馆。 amount 是一个顾客某一天的消费总额。
你是餐馆的老板,现在你想分析一下可能的营业额变化增长(每天至少有一位顾客)。
写一条 SQL 查询计算以 7 天(某日期 + 该日期前的 6 天)为一个时间段的顾客消费平均值。average_amount
要 保留两位小数。
查询结果按 visited_on
排序。
查询结果格式的例子如下。
示例 1:
输入: Customer 表: +-------------+--------------+--------------+-------------+ | customer_id | name | visited_on | amount | +-------------+--------------+--------------+-------------+ | 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 | +-------------+--------------+--------------+-------------+ 输出: +--------------+--------------+----------------+ | visited_on | amount | average_amount | +--------------+--------------+----------------+ | 2019-01-07 | 860 | 122.86 | | 2019-01-08 | 840 | 120 | | 2019-01-09 | 840 | 120 | | 2019-01-10 | 1000 | 142.86 | +--------------+--------------+----------------+ 解释: 第一个七天消费平均值从 2019-01-01 到 2019-01-07 是restaurant-growth/restaurant-growth/ (100 + 110 + 120 + 130 + 110 + 140 + 150)/7 = 122.86 第二个七天消费平均值从 2019-01-02 到 2019-01-08 是 (110 + 120 + 130 + 110 + 140 + 150 + 80)/7 = 120 第三个七天消费平均值从 2019-01-03 到 2019-01-09 是 (120 + 130 + 110 + 140 + 150 + 80 + 110)/7 = 120 第四个七天消费平均值从 2019-01-04 到 2019-01-10 是 (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150)/7 = 142.86
原站题解
pythondata 解法, 执行用时: 381 ms, 内存消耗: 67.3 MB, 提交时间: 2024-05-27 11:22:30
import pandas as pd def restaurant_growth(customer: pd.DataFrame) -> pd.DataFrame: # 统计每天营业额 tmp = customer.groupby('visited_on')['amount'].sum() amount = tmp.rolling(window=7).sum().round(2) average_amount = tmp.rolling(window=7).mean().round(2) result = pd.merge(amount, average_amount, on='visited_on') result.columns=['amount', 'average_amount'] result.reset_index(inplace=True) result.dropna(inplace=True) return result def restaurant_growth2(customer: pd.DataFrame) -> pd.DataFrame: df = customer.groupby('visited_on')['amount'].sum() amount = df.rolling(window = 7).sum().round(2) average_amount = df.rolling(window = 7).mean().round(2) result = pd.merge(amount,average_amount, on = 'visited_on').dropna().reset_index().rename(columns = { 'amount_x':'amount','amount_y': 'average_amount' }) return result
mysql 解法, 执行用时: 223 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 12:06:02
# Write your MySQL query statement below SELECT visited_on, amount, average_amount FROM (SELECT visited_on, SUM(amount) OVER (ORDER BY visited_on ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS amount, ROUND(AVG(amount) OVER (ORDER BY visited_on ROWS BETWEEN 6 PRECEDING AND CURRENT ROW), 2) AS average_amount, ROW_NUMBER() OVER (ORDER BY visited_on) AS rn FROM (SELECT DISTINCT visited_on, SUM(amount) over (PARTITION BY visited_on ORDER BY visited_on) AS amount FROM Customer ) a ) b WHERE b.rn >= 7
mysql 解法, 执行用时: 228 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 12:05:32
SELECT visited_on,amount,average_amount FROM ( SELECT visited_on, SUM(amount) OVER (ORDER BY visited_on ROWS 6 PRECEDING) AS amount, ROUND(AVG(amount)OVER(ORDER BY visited_on ROWS 6 PRECEDING),2) AS average_amount FROM ( SELECT visited_on,SUM(amount) AS amount FROM Customer GROUP BY visited_on ) TABLE_1 ) TABLE_2 WHERE DATEDIFF(visited_on,(SELECT MIN(visited_on) FROM Customer)) >=6
mysql 解法, 执行用时: 238 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 12:05:20
select c1.visited_on visited_on,sum(c2.amount) amount,round(sum(c2.amount)/7,2) average_amount from ( select visited_on,sum(amount) amount from Customer group by visited_on ) c1, ( select visited_on,sum(amount) amount from Customer group by visited_on ) c2 where datediff(c1.visited_on,c2.visited_on) <= 6 and datediff(c1.visited_on,c2.visited_on) >= 0 and datediff(c1.visited_on,(select min(visited_on) from Customer)) >=6 group by c1.visited_on
mysql 解法, 执行用时: 239 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 12:04:37
# Write your MySQL query statement below SELECT DISTINCT visited_on, sum_amount AS amount, ROUND(sum_amount/7, 2) AS average_amount -- 以上是破解【绊子1】并计算平均值,少用一次窗口函数提高运行速度 FROM ( SELECT visited_on, SUM(amount) OVER ( ORDER BY visited_on ROWS 6 PRECEDING ) AS sum_amount -- 以下是计算每天的金额总量,破解【绊子2】 FROM ( SELECT visited_on, SUM(amount) AS amount FROM Customer GROUP BY visited_on ) TT ) LL -- 最后手动只要覆盖完整7天的数据,破解【绊子1】 WHERE DATEDIFF(visited_on, (SELECT MIN(visited_on) FROM Customer)) >= 6