题解 | 统计每个产品的销售情况

描述

为了对每个产品的营销进行新的策划,需要统计2023年每个产品的销售情况。现有三个原始数据表格:customers(顾客)、products(产品)和orders(订单),其结构如下:

customers(顾客)

字段名 数据类型 说明
customer_id(顾客ID) 整数 顾客的唯一标识符
customer_name(顾客姓名) 字符串(最大长度50) 顾客的姓名
customer_email(顾客邮箱) 字符串(最大长度50) 顾客的电子邮箱地址
customer_age(顾客年龄) 整数 顾客的年龄
PRIMARY KEY (customer_id) - 将customer_id设置为主键,确保每个顾客ID的唯一性。

products(产品)

字段名 数据类型 说明
product_id(产品ID) 整数 产品的唯一标识符
product_name(产品名称) 字符串(最大长度50) 产品的名称
unit_price(单价) 十进制数(保留两位小数) 产品的单价
PRIMARY KEY (product_id) - 将product_id设置为主键,确保每个产品ID的唯一性。

orders(订单)

字段名 数据类型 说明
order_id(订单ID) 整数 订单的唯一标识符
customer_id(顾客ID) 整数 顾客的ID,对应customers表格中的customer_id
product_id(产品ID) 整数 产品的ID,对应products表格中的product_id
quantity(数量) 整数 产品的数量
order_date(订单日期) 日期 订单的日期
PRIMARY KEY (order_id) - 将order_id设置为主键,确保每个订单ID的唯一性。

查询要求

根据上述表格,查询2023年每个产品的以下信息:

  • 产品IDproduct_id):产品的ID。
  • 总销售额total_sales):该产品的2023年总销售额。
  • 单价unit_price):产品的单价。
  • 总销量total_quantity):该产品的2023年总销售数量。
  • 月平均销售额avg_monthly_sales):2023年该产品的月均销售额。
  • 单月最高销量max_monthly_quantity):2023年该产品的最大月销售数量。
  • 购买量最多的客户年龄段customer_age_group):2023年购买该产品数量最多的顾客的年龄段(1-10,11-20,21-30,31-40,41-50,51-60,61+)

排序规则

  • 按照每个产品的总销售额降序排列。
  • 如果总销售额一致,则按照产品的ID升序排列。
  • 当存在两个客户年购买量都是最高时,customer_age_group展示年龄小的顾客的年龄段。

计算说明

  • 总销售额 = 总销量 × 单价
  • 月平均销售额 = 总销售额 / 12
  • 所有计算结果保留两位小数。

【示例】

customers(顾客)表格

products(产品)表格

orders(订单)表格

按要求查询出来的结果

示例说明

假设产品104的2023年销售总量是6,单价是120.00,则:

  • 总销售额 = 6 × 120 = 720.00
  • 月平均销售额 = 720 / 12 = 60.00
  • 购买量最大的客户ID是2的Bob,年龄是30,所在年龄段是21-30。

示例1

输入:

复制代码
drop table if exists customers ;
drop table if exists products ;
drop table if exists orders ;
CREATE TABLE customers (
    customer_id INT,
    customer_name VARCHAR(50),
    customer_email VARCHAR(50),
    customer_age INT,
    PRIMARY KEY (customer_id)
);

INSERT INTO customers (customer_id, customer_name, customer_email, customer_age) VALUES
(1, 'Alice', 'alice@example.com', 25),
(2, 'Bob', 'bob@example.com', 30),
(3, 'Charlie', 'charlie@example.com', 22),
(4, 'David', 'david@example.com', 18),
(5, 'Eve', 'eve@example.com', 35);

CREATE TABLE products (
    product_id INT,
    product_name VARCHAR(50),
    unit_price DECIMAL(10, 2),
    PRIMARY KEY (product_id)
);

INSERT INTO products (product_id, product_name, unit_price) VALUES
(101, 'Product A', 50.00),
(102, 'Product B', 75.00),
(103, 'Product C', 100.00),
(104, 'Product D', 120.00),
(105, 'Product E', 90.00);

CREATE TABLE orders (
    order_id INT,
    customer_id INT,
    product_id INT,
    quantity INT,
    order_date DATE,
    PRIMARY KEY (order_id)
);

INSERT INTO orders (order_id, customer_id, product_id, quantity, order_date) VALUES
(1, 1, 101, 2, '2023-01-15'),
(2, 2, 102, 3, '2023-02-20'),
(3, 3, 103, 1, '2023-03-10'),
(4, 4, 104, 2, '2023-04-05'),
(5, 5, 105, 4, '2023-05-12'),
(6, 1, 102, 2, '2023-06-18'),
(7, 2, 103, 3, '2023-07-22'),
(8, 3, 104, 1, '2023-08-30'),
(9, 4, 105, 2, '2023-09-14'),
(10, 5, 101, 4, '2023-10-25'),
(11, 1, 103, 2, '2023-11-08'),
(12, 2, 104, 3, '2023-12-19');

复制输出:

复制代码
product_id|total_sales|unit_price|total_quantity|avg_monthly_sales|max_monthly_quantity|customer_age_group
104|720.00|120.00|6|60.00|3|21-30
103|600.00|100.00|6|50.00|3|21-30
105|540.00|90.00|6|45.00|4|31-40
102|375.00|75.00|5|31.25|3|21-30
101|300.00|50.00|6|25.00|4|31-40
sql 复制代码
select product_id,round(total_quantity*unit_price,2) as total_sales,unit_price,total_quantity,
       round(round(total_quantity*unit_price,2)/12,2) as avg_monthly_sales,
       quantity as max_monthly_quantity,
       customer_age_group
from (
    select  p.product_id,unit_price ,month(order_date)  as month,if(customer_age>1 and  customer_age<10,'1-10',if(customer_age<21,'11-20',if(customer_age<31,'21-30',if(customer_age<41,'31-40',if(customer_age<51,'41-50',if(customer_age<61,'51-60','61+')))))) as customer_age_group,customer_age,
                   dense_rank() over (partition by p.product_id order by quantity desc,customer_age   ) as rk,
                   sum(quantity) over (partition by p.product_id) as total_quantity,
                   quantity
    from orders join  products p on orders.product_id = p.product_id
    join customers c on orders.customer_id = c.customer_id
    group by  p.product_id,  unit_price, month(order_date),customer_age,quantity
     ) as t
where  rk=1
order by total_sales desc
相关推荐
写代码的【黑咖啡】2 小时前
Apache Flink SQL 入门与常见问题解析
sql·flink·apache
不穿格子的程序员2 小时前
Redis篇1——Redis深度剖析:从 5 种对象到 6 大底层结构
数据库·redis·缓存·redis五大数据类型·redis六大数据结构
Logic1012 小时前
《Mysql数据库应用》 第2版 郭文明 实验2 数据查询操作核心操作与思路解析
数据库·sql·mysql·学习笔记·计算机网络技术·形考作业·国家开放大学
小满、2 小时前
Redis:安装、主从复制、Sentinel 哨兵、Cluster 集群
数据库·redis·redis cluster·redis sentinel·redis 主从复制
白帽子黑客杰哥2 小时前
CTF Web题目常用考点与解题技巧合集
前端·数据库·web安全·网络安全·ctf·信息收集
CN-Dust2 小时前
MongoDB|Windows版安装教程(附资源)
数据库·windows·mongodb
落日漫游2 小时前
MySQL vs Redis vs MongoDB:三大数据库
数据库·redis·sql
程序边界2 小时前
金仓数据库助力Oracle迁移的深度体验:PL/SQL与函数支持全解析
数据库·sql·oracle
杜子不疼.2 小时前
AI Ping:大模型时代的“性能罗盘”——从选型到落地的全流程指南
数据库·人工智能·redis