当前位置:网站首页>【每日SQL打卡】DAY 26丨餐馆营业额变化增长【难度中等】
【每日SQL打卡】DAY 26丨餐馆营业额变化增长【难度中等】
2022-07-29 11:42:00 【不吃西红柿】
难度中等
SQL架构
表: 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边栏推荐
- Design and implementation of gbase8s Informix dodker high availability cluster self recovery cluster startup command oninitdb
- 什么是 Kubernetes 自定义资源定义 (CRD)?
- CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主
- 593. 有效的正方形 : 简单几何运用题
- Proficient in audio and video development can really do whatever you want
- Use anyio instead of asyncio
- c语言:来实现一个小程序n子棋(已五子棋为例)
- 幸运抽奖系统带后台源码
- 【无标题】
- 如何使用 grep 跨多行查找模式匹配
猜你喜欢

8. Interleave - understand ThreadPoolExecutor thread pool from architecture design to practice

宝塔快速搭建自适应咖啡网站模板与管理系统源码实测

593. 有效的正方形

mapbox 地图 生成矢量数据圆

2.2选择排序

One click blog building: how to use WordPress plug-in to build a dedicated blog

std::vector 拷贝、追加、嵌套访问

CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主

ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching

自采集在线电脑壁纸php源码v2.0自适应端
随机推荐
"100 Interview Knowledge Collections" 1. Interview Skills丨Do you really understand HR's careful thinking?
【一起学Rust | 基础篇】rust函数与流程控制详解
怎么以管理员身份运行cmd?以管理员身份运行cmd方法介绍
【年中总结】创业3年,越来越穷,还是坚持架构平台
企业微信客户朋友圈一天可以发多少条?都有哪些限制?如何突破朋友圈可展示人数限制?
Applied practical skills of deep reinforcement learning
小笑授权系统V5.0开心版
AI全流程开发难题破解之钥
【无标题】
报表查询字段集sql摘记
面试官培训课件(非常实用的企业内训课件)
为什么应该在开发环境中使用 Kubernetes
INVALID_ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
Collections.singletonList(T o)
ECCV 2022 | SSP: 自支持匹配的小样本任务新思想
Insights into the development of the enterprise live broadcast industry in 2022
puzzle(017.5)联动归位
解决 Chrome 浏览器被毒霸篡改问题
fastjson使用方法
微信发红包测试用例