首页 > 试题广场 >

电商平台想要了解不同商品在不同月份的销售趋势

[编程题]电商平台想要了解不同商品在不同月份的销售趋势
  • 热度指数:889 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
【背景】:电商平台想要了解不同商品在不同月份的销售趋势,以便进行库存管理和营销决策。
【原始表】:
products_underline (商品)表:
  • product_id (商品 ID): 商品的唯一标识符,INT
  • product_name (商品名称): 商品的名称,VARCHAR(50)
  • price (价格): 商品的单价,DECIMAL(10, 2)
sales_underline (销售)表:
  • sale_id (销售 ID): 销售的唯一标识符,INT
  • product_id (商品 ID): 商品的唯一标识符,用于关联商品表中的商品,INT
  • sale_month (销售月份): 销售发生的月份,VARCHAR(7)(格式:'YYYY-MM')
  • quantity (销售数量): 销售的商品数量,INT
【要求】:根据上述表格,查询出每个商品在 2024 年上半年(1 月至 6 月)的总销量、销量最高的月份的销售量、销量最低的月份的销售量、平均月销售量。查询结果按照商品 ID 升序排列。要求查询出来的表格的字段如下:
  • product_name: 商品的名称。
  • total_sales: 2024 年上半年(1-6月份)的总销售量。
  • max_monthly_sales:2024 年上半年(1-6月份)的总销售量销量最高的月份的销量
  • min_monthly_sales:2024 年上半年(1-6月份)的总销售量销量最低的月份的销量
  • avg_monthly_sales:2024 年上半年(1-6月份)的月平均销量(round保留整数)
【示例】
products_underline (商品)表:

sales_underline (销售)表:
【按要求查询出来的表】
【解释】
对于产品ID 1的产品,和产品ID 1 有关的订单有3笔,分别分散在2024年的1、2、3月份,所以总销量是100+150+120 = 370;销量最高的月份是2月份,销量是150;销量最低的月份是1月份,销量是100;月平均销量是370 / 3 约123;
示例1

输入

DROP TABLE IF EXISTS products_underline;
DROP TABLE IF EXISTS sales_underline;

-- 创建商品表
CREATE TABLE products_underline (
    product_id INT PRIMARY KEY,
    product_name VARCHAR(50),
    price DECIMAL(10, 2)
);

-- 创建销售表
CREATE TABLE sales_underline (
    sale_id INT PRIMARY KEY,
    product_id INT,
    sale_month VARCHAR(7),
    quantity INT
);

-- 插入测试数据
INSERT INTO products_underline (product_id, product_name, price) VALUES
(1, 'Product A', 10.99),
(2, 'Product B', 20.50),
(3, 'Product C', 15.75);

INSERT INTO sales_underline (sale_id, product_id, sale_month, quantity) VALUES
(1, 1, '2024-01', 100),
(2, 1, '2024-02', 150),
(3, 1, '2024-03', 120),
(4, 2, '2024-01', 80),
(5, 2, '2024-02', 90),
(6, 2, '2024-03', 110),
(7, 3, '2024-01', 50),
(8, 3, '2024-02', 70),
(9, 3, '2024-03', 60);
select * from products_underline;
select * from sales_underline;

输出

product_id|product_name|total_sales|max_monthly_sales|min_monthly_sales|avg_monthly_sales
1|Product A|370|150|100|123
2|Product B|280|110|80|93
3|Product C|180|70|50|60
头像 牛客794288350号
发表于 2025-06-17 23:47:22
select product_id, product_name, round(sum(sales),0) as total_sales, round(max(sales),0) as max_monthly_sales, round(min(sales),0) 展开全文
头像 这就开摆的蚊不叮很亲切
发表于 2025-06-17 15:27:43
select s.product_id, product_name, sum(quantity) as total_sales, max(quantity) as max_monthly_sales, min(quantity) as min_monthly_ 展开全文
头像 好事正酿
发表于 2025-06-17 22:07:27
with t1 as ( select product_id,sum(quantity) as total_sales,cast(sum(quantity)/count(sale_month) as signed) as avg_monthly_sales from sales_underline 展开全文
头像 yyyylx
发表于 2025-06-15 18:28:42
with t1 as ( select p.product_id, p.product_name, sum(s.quantity) as total_sales, round(sum(s.quantity) / count(d 展开全文
头像 好奇的黑眼圈想当offer收割机
发表于 2025-07-01 11:26:15
select p.product_id, product_name, sum(quantity) as total_sales, max(quantity) as max_monthly_sales, min(quantity) as min_monthly_ 展开全文
头像 牛客479560235号
发表于 2025-07-01 16:26:58
-- 逻辑拆解:时间条件2024年上半年,需要聚合的字段:按商品分组的总销量+月均销量+单月最高+单月最低 WITH monthly_total_max AS( SELECT t1.product_id, t2.quantity, DENSE 展开全文
头像 牛客917810122号
发表于 2025-06-21 16:05:24
with t1 as (select a.product_id product_id,product_name,sale_month,quantity from sales_underline a join products_underline b on a.product_id = b.pro 展开全文