首页 > 试题广场 >

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

[编程题]电商平台想要了解不同商品在不同月份的销售趋势
  • 热度指数: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
select products_underline.product_id,
    product_name,
    sum(max_monthly) as total_sales,
    max(max_monthly) as max_monthly_sales,
    min(max_monthly) as min_monthly_sales,
    round(avg(max_monthly),0) as avg_monthly_sales
from products_underline
join (
    select product_id,
        cast(right(sale_month,2) as signed) as s_month,
        sum(quantity) as max_monthly
    from sales_underline
    group by 1,2
    having s_month between 1 and 6
) t
using (product_id)
group by 1,2
order by 1,3 desc

发表于 2025-06-20 18:19:14 回复(1)
select product_id,product_name,sum(quantity) as total_sales,max(quantity) as max_monthly_sales, min(quantity) as min_monthly_sales, round(avg(quantity),0) as avg_monthly_sales
from products_underline
join sales_underline using(product_id)
where sale_month between '2024-01' and '2024-06'
group by product_id,product_name
order by product_id
发表于 2025-06-17 00:38:59 回复(0)
with total_sales_tb as(
    select
        pu.product_name
        , sum(su.quantity) as total_sales
    from
        sales_underline as su
        left join products_underline as pu on su.product_id = pu.product_id
    where
        sale_month between '2024-01' and '2024-06'
    group by
        pu.product_name
)
, max_monthly_sales_tb as(
    select
        pu.product_name
        , max(su.quantity) as max_monthly_sales
    from
        sales_underline as su
        left join products_underline as pu on su.product_id = pu.product_id
    where
        sale_month between '2024-01' and '2024-06'
    group by
        pu.product_name
)
, min_monthly_sales_tb as(
    select
        pu.product_name
        , min(su.quantity) as min_monthly_sales
    from
        sales_underline as su
        left join products_underline as pu on su.product_id = pu.product_id
    where
        sale_month between '2024-01' and '2024-06'
    group by
        pu.product_name
)
, avg_monthly_sales_tb as(
    select
        pu.product_name
        , round(avg(su.quantity), 0) as avg_monthly_sales
    from
        sales_underline as su
        left join products_underline as pu on su.product_id = pu.product_id
    where
        sale_month between '2024-01' and '2024-06'
    group by
        pu.product_name
)

select
    pu.product_id
    , pu.product_name
    , ts_tb.total_sales
    , mms_tb.max_monthly_sales
    , mms1_tb.min_monthly_sales
    , ams_tb.avg_monthly_sales
from
    products_underline as pu
    left join total_sales_tb as ts_tb on pu.product_name = ts_tb.product_name
    left join max_monthly_sales_tb as mms_tb on pu.product_name = mms_tb.product_name
    left join min_monthly_sales_tb as mms1_tb on pu.product_name = mms1_tb.product_name
    left join avg_monthly_sales_tb as ams_tb on pu.product_name = ams_tb.product_name
order by
    pu.product_id

发表于 2025-07-02 17:23:54 回复(0)
with
    k as (
        select
            a.product_id,
            product_name,
            sum(quantity) total_sales,
            max(quantity) max_monthly_sales,
            min(quantity) min_monthly_sales,
            round(avg(quantity),0) avg_monthly_sales
        from
            products_underline a
            left join sales_underline b on a.product_id = b.product_id
        where
            sale_month between "2024-01" and "2024-06"
        group by
            a.product_id,
            product_name
    )
select
*
from
    k

发表于 2025-06-30 14:36:02 回复(0)
with t as (
    select product_id,
           sale_month,
           sum(quantity) as monthly_sales
    from sales_underline s
    where sale_month between '2024-01'and '2024-06'
    group by product_id,sale_month),

b as (
    select s.product_id,
           p.product_name,
           sum(s.quantity) as total_sales,
           round(sum(s.quantity)/count(distinct s.sale_month),0) as avg_monthly_sales
from sales_underline  as s
left join products_underline as p 
 on s.product_id=p.product_id
where s.sale_month between '2024-01'and '2024-06'
group by s.product_id,p.product_name)

select b.product_id,
       b.product_name,
       b. total_sales,
       max(t.monthly_sales) as max_monthly_sales,
       min(t.monthly_sales) as min_monthly_sales,
       b.avg_monthly_sales
from b
left join t on b.product_id=t.product_id

group by b.product_id,b.product_name,b. total_sales
order by b.product_id

发表于 2025-06-24 15:35:22 回复(0)
WITH t1 AS(
    SELECT product_id, 
        quantity,
        ROW_NUMBER() OVER(PARTITION BY product_id ORDER BY quantity DESC) AS rk_high
    FROM sales_underline
),
t2 AS(
    SELECT product_id, 
        quantity,
        ROW_NUMBER() OVER(PARTITION BY product_id ORDER BY quantity ) AS rk_low
    FROM sales_underline
)
SELECT 
    pu.product_id,
    product_name,
    SUM(su.quantity) AS total_sales,
    t1.quantity AS max_monthly_sales,
    t2.quantity AS min_monthly_sales,
    ROUND(AVG(su.quantity), 0) AS avg_monthly_sales
FROM products_underline pu
JOIN sales_underline su ON(pu.product_id = su.product_id)
JOIN t1 ON(su.product_id = t1.product_id)
JOIN t2 ON(su.product_id = t2.product_id)
WHERE rk_high = 1 AND rk_low = 1 AND sale_month BETWEEN '2024-01' AND '2024-06'
GROUP BY pu.product_id, product_name, t1.quantity, t2.quantity
用窗口函数写了一下
发表于 2025-06-24 00:27:42 回复(0)
with
    month_sales as (
        select
            p.product_id,
            p.product_name,
            month(s.sale_month) as month,
            sum(p.price * s.quantity) as monthly_sales
        from
            products_underlin as p
            join sales_underline as s on p.product_id = s.product_id
        where
            s.sale_month between '2024-01' and '2024-06'
        group by
            p.product_id,
            p.product_name,
            month(s.sale_month)
    )
select
    m.product_id,
    m.product_name,
    sum(m.monthly_sales) as total_sales,
    max(m.monthly_sales) as max_monthly_sales,
    min(m.monthly_sales) as min_monthly_sales,
    round(avg(m.monthly_sales),0) as avg_monthly_sales
from
    month_sales as m
group by
    m.product_id,
    m.product_name
order by
    m.product_id

到底哪错了呀,一直运行错误
发表于 2025-06-17 15:27:07 回复(3)