首页 > 试题广场 >

每个商品的销售总额

[编程题]每个商品的销售总额
  • 热度指数:81614 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
假设你是一个电商平台的数据库工程师,需要编写一个SQL查询来生成每个商品的销售排行榜。你的数据库中有products和orders两张表:
products示例表如下,包括product_id(商品编号)、name(商品名称)和category(商品类别)字段;
product_id name category
1 Product A Category 1
2 Product B Category 1
3 Product C Category 2
4 Product D Category 2
5 Product E Category 3
orders示例表如下,包括order_id(订单编号)、product_id(商品编号)、quantity(销售数量)和order_date(下单日期)字段;
order_id product_id quantity order_date
101 1 5 2023-08-01
102 2 3 2023-08-01
103 3 8 2023-08-02
104 4 10 2023-08-02
105 5 15 2023-08-03
106 1 7 2023-08-03
107 2 4 2023-08-04
108 3 6 2023-08-04
109 4 12 2023-08-05
110 5 9 2023-08-05
使用上述表格,编写一个SQL查询,返回每个商品的销售总量,先按照商品类别升序排序,再按销售总量降序排列,同时包括商品名称和销售总量。此外,还需要在结果中包含每个商品在其所属类别内的排名,排名相同的商品可以按照 product_id 升序排序。
示例输出如下:
product_name total_sales category_rank
Product A 12 1
Product B 7 2
Product D 22 1
Product C 14 2
Product E 24 1
示例1

输入

drop table if exists products;
CREATE TABLE products (
    product_id INT PRIMARY KEY,
    name VARCHAR(255),
    category VARCHAR(255)
);
-- 插入商品数据
INSERT INTO products (product_id, name, category)
VALUES
    (1, 'Product A', 'Category 1'),
    (2, 'Product B', 'Category 1'),
    (3, 'Product C', 'Category 2'),
    (4, 'Product D', 'Category 2'),
    (5, 'Product E', 'Category 3');


drop table if exists orders;
CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    product_id INT,
    quantity INT,
    order_date DATE
);
-- 插入订单数据
INSERT INTO orders (order_id, product_id, quantity, order_date)
VALUES
    (101, 1, 5, '2023-08-01'),
    (102, 2, 3, '2023-08-01'),
    (103, 3, 8, '2023-08-02'),
    (104, 4, 10, '2023-08-02'),
    (105, 5, 15, '2023-08-03'),
    (106, 1, 7, '2023-08-03'),
    (107, 2, 4, '2023-08-04'),
    (108, 3, 6, '2023-08-04'),
    (109, 4, 12, '2023-08-05'),
    (110, 5, 9, '2023-08-05');

输出

product_name|total_sales|category_rank
Product A|12|1
Product B|7|2
Product D|22|1
Product C|14|2
Product E|24|1
select 
    name as product_name,
    sum(quantity) as total_sales,
    row_number() over(partition by category order by sum(quantity) desc) as category_rank
from orders o
inner join products p
    on o.product_id = p.product_id
group by name, category

注意用的是 inner join,left join 会返回所有匹配记录,不符合条件的右表会自动填充为 null
发表于 2024-09-08 17:19:36 回复(4)
SELECT product_name, 
       total_sales, 
       category_rank
  FROM  
   (SELECT *,
         RANK() OVER(PARTITION BY category ORDER BY total_sales DESC) AS category_rank
     FROM
     (
        SELECT p.name AS product_name,
               p.category,
               SUM(IFNULL(o.quantity,0)) AS total_sales
          FROM products p 
     LEFT JOIN orders o 
            ON p.product_id = o.product_id
      GROUP BY p.name, p.category) AS t
   ORDER BY category, total_sales DESC) AS t2;

我以为要显示所有的product, 呃呃呃呃
发表于 2024-08-11 05:49:30 回复(0)
分步来做

with t1 as(
    select o.*, p.name as product_name, p.category
    from products p
    join orders o 
    on p.product_id=o.product_id
)
,
t2 as(
    select product_id, product_name, category,
    sum(quantity) as total_quantity
    from t1
    group by product_id, product_name, category
),
t3 as(
    select *, row_number() over(partition by category order by total_quantity desc, product_id asc) as category_rank
    from t2
) 

select product_name, sum(total_quantity)  as total_sales, min(category_rank) as category_rank
from t3
group by product_name


发表于 2025-04-26 09:16:21 回复(0)
select name as product_name,
sum(quantity) as total_sales,
rank()over(partition by category order by sum(quantity) desc) as category_rank
from orders o
inner join products p
on p.product_id = o.product_id
group by product_name,category

发表于 2025-04-09 17:13:15 回复(3)
select name as product_name, sum(quantity) as total_sales, dense_rank() over (partition by category order by sum(quantity) desc) as category_rank
from products join orders on products.product_id = orders.product_id
group by product_name, category, products.product_id
order by category, total_sales desc, products.product_id

发表于 2024-09-14 13:22:23 回复(1)
SELECT
    p.name AS product_name,
    SUM(o.quantity) AS total_sales,
    ROW_NUMBER() OVER (PARTITION BY p.category ORDER BY SUM(o.quantity) DESC, p.product_id ASC) AS category_rank
FROM
    products p
    INNER JOIN orders o ON p.product_id = o.product_id
GROUP BY
    p.name, p.category, p.product_id

发表于 2024-08-14 15:14:54 回复(0)
with t1 as(
select
p.product_id,
name,
category,
sum(quantity) total_sales
from
products p join orders o
on p.product_id = o.product_id
group by name,category,p.product_id
)
select
name as product_name,
total_sales,
rank() over(partition by category order by total_sales desc,product_id ) category_rank
from t1
;

发表于 2024-08-09 21:04:42 回复(0)
select p.name as product_name
      ,o.sum_quantity as total_sales
      ,row_number() over (
          partition by p.category
          order by o.sum_quantity desc, p.product_id asc
      ) as category_rank
  from products p
  left join (
      select product_id
            ,IFNULL(sum(quantity), 0) as sum_quantity
      from orders
      group by product_id
  ) o on p.product_id = o.product_id
order by p.category asc, o.sum_quantity desc


为什么显示2/3组用例通过,哪里还有问题
发表于 2026-02-27 21:31:18 回复(0)
select
    b.name as product_name,
    sum(quantity) as total_sales,
    row_number()over(partition by category order by category,sum(quantity) desc,b.product_id) as category_rank
from orders a  join products b
on a.product_id=b.product_id
group by b.name,category,b.product_id
发表于 2025-10-07 12:30:55 回复(0)
select
    product_name,
    total_sales,
    rank() over (partition by category order by  total_sales desc  ) as category_rank   
from
    (
        select
            name as product_name,
            sum(quantity) as total_sales,
            category
        from orders
        inner join products using (product_id)
        group by
            product_name,
            category
    ) as e

发表于 2025-08-17 17:28:45 回复(0)
select p.name product_name, sum(o.quantity) total_sales,
rank()over(partition by p.category order by sum(o.quantity) desc) category_rank
from products p  join orders o
on(p.product_id = o.product_id)
group by p.name,category
order by category, total_sales desc
发表于 2025-05-01 19:46:33 回复(0)
注意排序规则,题目全一股脑说了
select 
       name as product_name,
       total_sales,
       dense_rank()over(partition by category order by total_sales desc,product_id) as category_rank
from(
select category,name,a.product_id,sum(quantity) as total_sales
from orders a
inner join products b using(product_id)
group by category,name,a.product_id
order by category)c



发表于 2025-03-18 13:22:50 回复(2)
SELECT
    name product_name,
    SUM(quantity) total_sales,
    ROW_NUMBER()OVER(PARTITION BY category ORDER BY SUM(quantity) DESC, orders.product_id) category_rank
FROM
    products
    JOIN
    orders
    ON
    products.product_id = orders.product_id
GROUP BY
    orders.product_id
ORDER BY
    category,
    total_sales DESC

发表于 2024-08-16 00:18:53 回复(0)
select name product_name, total_sales, 
rank() over(partition by category order by total_sales desc) category_rank
from products p join (select product_id, sum(quantity) total_sales
from orders
group by product_id) o using(product_id)
order by category,total_sales desc,product_id

发表于 2024-08-09 17:10:09 回复(0)
select
    product_name,
    total_sales,
    category_rank
from
    (
        select
            t1.product_id as product_id,
            t1.name as product_name,
            sum(t2.quantity) as total_sales,
            t1.category as category,
            row_number() over (
                partition by
                    t1.category
                order by
                    sum(t2.quantity) desc
            ) as category_rank
        from
            products as t1
            join orders as t2 on t1.product_id = t2.product_id
        GROUP BY
            t1.product_id,
            t1.name
    ) as a
order by
    category asc,
    total_sales desc
发表于 2024-06-26 13:32:04 回复(0)
select
    p.name as product_name,
    sum(o.quantity) as total_sales,
    row_number() over (
        partition by
            p.category
        order by
            sum(o.quantity) desc,
            p.product_id ASC
    ) as category_rank
from
    products p
    join orders o on p.product_id = o.product_id
group by
    p.product_id,
    p.name,
    p.category
order by
    p.category,
    total_sales desc

发表于 2026-04-11 17:03:01 回复(0)
#每种商品品类的销售总量
#销售总量在其category里的排名
select
    p.name as product_name,
    total_sales,
    row_number() over (
        partition by
            category
        order by
            total_sales desc
    ) as category_rank
from
    products p
    join (
        select
            o.product_id,
            sum(o.quantity) as total_sales
        from
            orders o
        group by
            o.product_id
    ) n on p.product_id = n.product_id
order by
    p.category asc,
    total_sales desc,
    n.product_id asc 

发表于 2026-04-07 11:40:19 回复(0)
select 
    a.name as product_name,
    total_sales,
    row_number() over(partition by category order by total_sales desc) as category_rank
from products a
join (
    select 
        name,
        sum(quantity) as total_sales
    from orders t1
    left join products t2
    on t1.product_id = t2.product_id
    group by 1
) b
on a.name = b.name
 

发表于 2026-04-03 00:16:33 回复(0)
select t2.product_name,t2.total_sales,t2.category_rank
from
(
select product_name,total_sales,
 row_number() over(partition by category order by total_sales desc,product_id asc) as category_rank,category
from(
select p.name as product_name,sum(o.quantity) as total_sales,p.category,p.product_id
from products p 
right join orders o 
  on p.product_id = o.product_id
where p.name is not null
group by p.product_id, p.name, p.category
) as t1
) as t2
order by t2.category asc, t2.total_sales desc

发表于 2026-03-31 09:20:13 回复(0)
SELECT name AS product_name,
       SUM(quantity) AS total_sales,
       ROW_NUMBER() OVER (
           PARTITION BY category
           ORDER BY SUM(quantity) DESC
       ) AS category_rank
FROM products
INNER JOIN orders
ON products.product_id = orders.product_id
GROUP BY category, name
ORDER BY category ASC, total_sales DESC;
固执的不去子查询但不能严格按照最后的product_id排序
发表于 2026-03-30 14:06:54 回复(0)