首页 > 试题广场 >

哪些产品在特定时间段内表现最为出色

[编程题]哪些产品在特定时间段内表现最为出色
  • 热度指数:574 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
【背景】:公司需要对过去一段时间的销售情况进行深入分析,以了解哪些产品在特定时间段内表现最为出色。
【原始表】:
products(产品)表:
  • product_id (产品 ID): 产品的唯一标识符
  • product_name (产品名称): 产品的名称

sales_records(销售记录)表:
  • sales_id (销售 ID): 销售记录的唯一标识符
  • product_id (产品 ID): 产品的唯一标识符,用于关联产品表中的产品
  • sales_date (销售日期): 销售发生的日期
  • sales_amount (销售金额): 该次销售的金额
  • sales_quantity (销售数量): 该次销售的产品数量

【要求】:根据上面这两个表格,查询在'2024-01-01' —'2024-12-31'销量最高的产品,包含的字段:产品 ID、产品名称、总销售额、总销量。如果存在销量一样的多个产品,都展示出来且按照产品ID升序排列,要求查询出来的表格的字段如下:
  • product_id: 产品的唯一标识符。
  • product_name: 产品的名称。
  • total_sales_amount: 总销售额。
  • total_sales_quantity: 总销量。
【示例】:
products(产品)表:

sales_records(销售记录)表:
【按照要求查询出来的表】
【解释】:2024年的订单有4笔,其中产品ID是1和3的销量最高,都是16,所以查询出来产品ID是1和3的产品,他们的总销售金额也一样都是900
示例1

输入

DROP TABLE IF EXISTS products;
DROP TABLE IF EXISTS sales_records;
-- 创建表
CREATE TABLE products (
    product_id INT PRIMARY KEY,
    product_name VARCHAR(50)
);

CREATE TABLE sales_records (
    sales_id INT PRIMARY KEY,
    product_id INT,
    sales_date DATE,
    sales_amount DECIMAL(10, 2),
    sales_quantity INT
);

-- 插入数据
INSERT INTO products (product_id, product_name)
VALUES (1, '产品 A'),
       (2, '产品 B'),
       (3, '产品 C'),
       (4, '产品 D');

INSERT INTO sales_records (sales_id, product_id, sales_date, sales_amount, sales_quantity)
VALUES (1, 1, '2024-03-05', 500.00, 10),
       (2, 2, '2024-05-10', 300.00, 8),
       (3, 1, '2024-06-20', 400.00, 6),
       (4, 3, '2024-06-20', 900.00, 16);

select * from products;
select * from sales_records;

输出

product_id|product_name|total_sales_amount|total_sales_quantity
1|产品 A|900.00|16
3|产品 C|900.00|16
with t as (
select  p.product_id,
        p.product_name,
        sum(s.sales_amount) as total_sales_amount,
        sum(s.sales_quantity) as total_sales_quantity,
        dense_rank() over ( order by sum(s.sales_quantity) desc) as rn
       
from sales_records as s
left join products as p
on s.product_id=p.product_id

where year(s.sales_date)='2024'
group by  p.product_id)


select product_id,
       product_name,
       total_sales_amount,
       total_sales_quantity
from t
where rn=1
order by product_id

发表于 2025-06-27 17:40:57 回复(0)
select
product_id,product_name,total_sales_amount,total_sales_quantity
from (
    select
    a.product_id,product_name,total_sales_amount,total_sales_quantity,
    rank()over( order by total_sales_quantity desc) as rk
from (
    select product_id, sum(sales_amount) as total_sales_amount  
    , sum(sales_quantity) as  total_sales_quantity
    from sales_records
    where sales_date between '2024-01-01' and '2024-12-31'
    group by product_id
)a
left join products p using(product_id)
)b
where rk = 1;
发表于 2025-06-25 11:40:37 回复(0)
with temp as
(
    select
    a.product_id,a.product_name,
    sum(b.sales_amount) as total_sales_amount,
    sum(b.sales_quantity) as total_sales_quantity
    from products a
    join sales_records b
    on a.product_id=b.product_id
    where b.sales_date between '2024-01-01' and '2024-12-31'
    group by a.product_id,a.product_name
)

select
product_id,
product_name,
total_sales_amount,
total_sales_quantity
from
(
    select
    product_id,
    product_name,
    total_sales_amount,
    total_sales_quantity,
    dense_rank() over(order by total_sales_quantity desc) as ranking
    from temp
) t
where ranking=1
order by product_id

发表于 2025-06-23 19:37:02 回复(0)
with sales_summary as (
        select
            p.product_id,
            p.product_name,
            sum(s.sales_amount) as total_sales_amount,
            sum(s.sales_quantity) as total_sales_quantity
        from
            products as p
            left outer join sales_records as s on p.product_id = s.product_id
            and s.sales_date between '2024-01-01' AND '2024-12-31'
        group by
            p.product_id,
            p.product_name
    )
select
    product_id,
    product_name,
    total_sales_amount,
    total_sales_quantity    
from
    sales_summary
where
    total_sales_quantity = (
        select
            max(total_sales_quantity)
        from
            sales_summary
    )
order by
    product_id asc;

发表于 2025-06-22 11:06:40 回复(0)