首页 > 试题广场 >

分析每个商品在不同时间段的销售情况

[编程题]分析每个商品在不同时间段的销售情况
  • 热度指数:1830 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
【背景】:电商平台需要分析每个商品在不同时间段的销售情况,现在需要你根据下面三个数据表,查询出每个商品在 2024 年第二季度的销售总额、在不同类别中的销售排名以及所属供应商。
【原始表】:
product_info(商品信息)表:
  • product_id (商品 ID): 商品的唯一标识符
  • product_name (商品名称): 商品的名称
  • category (类别): 商品所属的类别

order_info(订单信息)表:
  • order_id (订单 ID): 订单的唯一标识符
  • product_id (商品 ID): 商品的唯一标识符,用于关联商品表中的商品
  • order_date (订单日期): 下单日期
  • total_amount (订单总额): 订单的总金额

supplier_info(供应商信息)表:
  • supplier_id (供应商 ID): 供应商的唯一标识符
  • product_id (商品 ID): 商品的唯一标识符,用于关联商品表中的商品
  • supplier_name (供应商名称): 供应商的名称

【要求】:根据上面这三个表格,查询每个商品的销售情况,包含的字段:商品 ID、商品名称、该商品在 2024 年第二季度的销售总额,该商品在所属类别的销售排名,所属供应商。查询出来的数据按照商品 ID 升序排列。要求查询出来的表格的字段如下:
  • product_id: 商品的唯一标识符。
  • product_name: 商品的名称。
  • q2_2024_sales_total: 2024 年第二季度的销售总额。4、5、6月份属于第二季度
  • category_rank: 在所属类别的销售排名。
  • supplier_name: 所属供应商。
【示例】
product_info(商品信息)表:
order_info(订单信息)表:
supplier_info(供应商信息)表:

【按要求查询出来的表】

【解释】
上述示例中,产品ID1、2、3、4的产品都是2024年第2季度产生了订单,产品A产生的订单总金额是1100,产品ID5产品E在2024年第2季度没有产生订单,产品A和产品E都是Clothing类别,所以产品A排名1,产品E排名2且销售总金额是0;
示例1

输入

DROP TABLE IF EXISTS product_info;
DROP TABLE IF EXISTS order_info;
DROP TABLE IF EXISTS supplier_info;

-- 创建表
CREATE TABLE product_info (
    product_id INT PRIMARY KEY,
    product_name VARCHAR(50),
    category VARCHAR(50)
);

CREATE TABLE order_info (
    order_id INT PRIMARY KEY,
    product_id INT,
    order_date DATE,
    total_amount DECIMAL(10, 2)
    
);

CREATE TABLE supplier_info (
    product_id INT PRIMARY KEY,
    supplier_id INT ,
    supplier_name VARCHAR(50)
   
);

-- 插入数据
INSERT INTO product_info (product_id, product_name, category)
VALUES (1, 'Product A', 'Electronic Products'),
       (2, 'Product B', 'Clothing'),
       (3, 'Product C', 'Clothing'),
       (4, 'Product D', 'Clothing'),
       (5, 'Product E', 'Electronic Products');

INSERT INTO order_info (order_id, product_id, order_date, total_amount)
VALUES (1, 1, '2024-04-01', 500),
       (2, 1, '2024-05-01', 600),
       (3, 2, '2024-04-01', 400),
       (4, 3, '2024-05-01', 300),
       (5, 4, '2024-05-01', 200),
       (6, 5, '2024-07-01', 300);

INSERT INTO supplier_info (supplier_id, product_id, supplier_name)
VALUES (1, 1, 'Supplier A'),
       (2, 2, 'Supplier B'),
       (2, 3, 'Supplier B'),
       (2, 4, 'Supplier B'),
       (2, 5, 'Supplier B');


select * from product_info;
select * from order_info;
select * from supplier_info;

输出

product_id|product_name|q2_2024_sales_total|category_rank|supplier_name
1|Product A|1100.00|1|Supplier A
2|Product B|400.00|1|Supplier B
3|Product C|300.00|2|Supplier B
4|Product D|200.00|3|Supplier B
5|Product E|0.00|2|Supplier B
with table01 as (
    select
        pi.product_id
        , pi.product_name
        , round(if(t1.q2_2024_sales_total is null, 0, t1.q2_2024_sales_total), 2) as q2_2024_sales_total
        , rank()over(partition by pi.category order by t1.q2_2024_sales_total desc) as category_rank
    from
    product_info as pi
    left join
        (select
            pi.product_id
            , pi.product_name
            , sum(oi.total_amount) as q2_2024_sales_total
        from
            product_info as pi
            left join order_info as oi on pi.product_id = oi.product_id
        where
            year(order_date) = 2024
            and month(order_date) >= 4
            and month(order_date) <= 6
        group by
            pi.product_id
            , pi.product_name
        ) as t1 on pi.product_id = t1.product_id
    order by
        pi.product_id
)

select
    t1.product_id
    , t1.product_name
    , t1.q2_2024_sales_total
    , t1.category_rank
    , si.supplier_name
from
    table01 as t1
    left join supplier_info as si on t1.product_id = si.product_id

发表于 2025-07-06 16:55:09 回复(0)
select p.product_id, p.product_name, ifnull(a.q2_2024_sales_total,'0.00') q2_2024_sales_total, dense_rank()over(partition by p.category order by q2_2024_sales_total DESC) category_rank, s.supplier_name
from product_info p
left join supplier_info s on p.product_id=s.product_id
left join (
    select product_id, (sum(total_amount)) q2_2024_sales_total
    from order_info o
    where order_date between '2024-04-01' and '2024-06-30'
    group by product_id
)a
on p.product_id=a.product_id
group by p.product_id, p.product_name, p.category, s.supplier_name, a.q2_2024_sales_total
order by p.product_id
发表于 2025-07-03 10:34:39 回复(0)
select p.product_id,
       p.product_name,
       SUM(IFNULL(o.total_amount, 0)) as q2_2024_sales_total,

       row_number() over (partition by p.category order by SUM(IFNULL(o.total_amount, 0)) desc) as category_rank,
       s.supplier_name

from product_info as p
left join order_info as o
ON p.product_id = o.product_id 
  and  MONTH(o.order_date) between 4 and 6
left join supplier_info as s
on p.product_id=s.product_id

group by p.product_id, p.product_name,s.supplier_name
order by p.product_id 
第一,用ifnull(      ,0)让销量为空的输出0。
第二,以product_info为主表,连接订单表时,把月份限制条件写在On子句中。(如果以订单表为主表,这时月份限制条件只能写在where子句中,这时会把没有订单的显示为null的行直接过滤掉,上面的ifnull也就没意义了。)
发表于 2025-06-24 17:59:34 回复(0)
SELECT
  pi.product_id,
  product_name,
  SUM(IFNULL(total_amount, 0)) AS q2_2024_sales_total,
  ROW_NUMBER() OVER (PARTITION BY category ORDER BY 
                     SUM(IFNULL(total_amount, 0)) DESC) AS category_rank,
  supplier_name
FROM product_info pi
LEFT JOIN order_info oi ON pi.product_id = oi.product_id AND 
                           MONTH(order_date) IN (4, 5, 6)
LEFT JOIN supplier_info si ON pi.product_id = si.product_id 
GROUP BY pi.product_id, product_name, supplier_name
ORDER BY pi.product_id

发表于 2025-06-24 16:25:15 回复(0)
WITH t1 AS(
    SELECT product_id, SUM(total_amount) AS q2_2024_sales_total
    FROM order_info
    WHERE order_date BETWEEN '2024-04-01' AND '2024-06-30'
    GROUP BY product_id
),
t2 AS(
    SELECT pi.product_id, category,
    RANK() OVER(PARTITION BY category ORDER BY q2_2024_sales_total DESC) AS category_rank
    FROM product_info pi
    LEFT JOIN t1 ON(pi.product_id = t1.product_id)
)
SELECT
    pi.product_id,
    product_name,
    IFNULL(q2_2024_sales_total, '0.00') AS q2_2024_sales_total,
    category_rank,
    supplier_name
FROM product_info pi
LEFT JOIN t1 ON(pi.product_id = t1.product_id)
LEFT JOIN t2 ON(pi.product_id = t2.product_id)
LEFT JOIN supplier_info si ON(pi.product_id = si.product_id)
ORDER BY pi.product_id
拆开写逻辑清晰一点
发表于 2025-06-24 01:02:37 回复(0)
SELECT
product_id
,product_name
,SUM(ifnull(total_amount,0)) AS q2_2024_sales_total
,ROW_NUMBER()OVER(PARTITION BY category ORDER BY SUM(ifnull(total_amount,0)) DESC )
AS category_rank
,supplier_name
FROM(select
A.product_id
,A.product_name
,case when DATE_FORMAT(B.order_date , '%c') in(4,5,6) then B.order_date
else 0
end as order_date
,case when DATE_FORMAT(B.order_date , '%c') in(4,5,6) then total_amount
else 0
end as total_amount
,category
,supplier_name
from
product_info A left JOIN order_info B ON A.product_id=B.product_id
left JOIN supplier_info C ON  A.product_id=C.product_id
) as t1
where DATE_FORMAT(order_date , '%c') in(4,5,6) or order_date=0
GROUP BY
product_id
,product_name
,supplier_name
ORDER BY product_id ASC
发表于 2025-06-22 12:00:24 回复(0)
with t4 as
(select t1.product_id,t1.product_name,sum(total_amount) as q2_2024_sales_total,supplier_name,category
from product_info t1
join order_info t2 using(product_id)
join supplier_info t3 using(product_id)
where order_date between '2024-04-01' and '2024-06-30'
group by t1.product_id,t1.product_name)

select t1.product_id,t1.product_name,ifnull(q2_2024_sales_total,0) as q2_2024_sales_total, rank() over (partition by t1.category order by q2_2024_sales_total desc) as category_rank, t3.supplier_name
from product_info t1 join supplier_info t3 using(product_id) left join t4 using(product_id)
order by product_id
发表于 2025-06-17 01:38:14 回复(0)
有家人知道销量为0 的 Product 怎么保留吗
发表于 2025-06-16 02:51:11 回复(3)