题目
表: 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 天)为一个时间段的顾客消费平均值
查询结果格式的例子如下:
查询结果按 visited_on 排序
average_amount 要 保留两位小数,日期数据的格式为 ('YYYY-MM-DD')
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 是 (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
解答
这不就很像窗口函数么
先选出最终表的visited_on
select C.visited_on
from Customer as C
where C.visited_on >= DATE_ADD((SELECT min(visited_on) FROM customer), INTERVAL 6 DAY);
然后与主表左连接
连接条件就是左表的时间要大于右表的时间 而且 右表的时间+6要大于左表的时间
select tmp.visited_on, sum(CC.amount) as amount,
avg(CC.amount) as average_amount
from (select C.visited_on
from Customer as C
where C.visited_on >= DATE_ADD((SELECT min(visited_on) FROM customer), INTERVAL 6 DAY)) as tmp
left join Customer as CC
on tmp.visited_on >= CC.visited_on and DATE_ADD(tmp.visited_on, INTERVAL 6 DAY) >= tmp.visited_on
group by tmp.visited_on
别的解答
先对每个表按日期进行去重求和,然后做笛卡尔积,然后筛选出符合条件的,再之后排序。得到答案
select c1.visited_on,sum(c2.amount) amount,round(sum(c2.amount)/7,2) average_amount
from
(select customer_id,name,visited_on,sum(amount) amount from Customer group by visited_on)c1 cross join
(select customer_id,name,visited_on,sum(amount) amount from Customer group by visited_on) c2
where datediff(c1.visited_on,c2.visited_on)<7 and datediff(c1.visited_on,c2.visited_on)>=0
group by c1.visited_on
having count(*)>6
order by c1.visited_on