首页 > 试题广场 >

每个商品的销售总额

[编程题]每个商品的销售总额
  • 热度指数:38368 时间限制: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
头像 牛客题解官
发表于 2025-02-20 11:05:39
精华题解 这道题目要求我们计算每个商品的销售总量,下面是这个SQL查询的思路和实现步骤。 拆解题目 1. 确定总体问题 目标是计算每个商品的销售总量,并按商品的还款能力级别对其进行排名。输出商品名称、销售总量和在其类别中的排名,并按还款能力级别和排名进行排序。 2. 分析关键问题 连接表:我们需要将prod 展开全文
头像 游客_1004
发表于 2024-08-21 21:10:02
这题真的莫名奇妙,第三组测试数据会发生product_name为None的情况,所以用join而不是left join
头像 拒绝996的小蜗牛很自信
发表于 2024-08-09 21:46:44
select name as product_name, sum(quantity) as total_sales, row_number()over(partition by category order by category,sum(quantity) desc) as category_r 展开全文
头像 笑一下a
发表于 2024-08-10 20:11:18
select name product_name,sum(quantity) total_sales,rank()over(partition by category order by sum(quantity)desc) category_rank from products a join ord 展开全文
头像 纯真的铁锤不愿再收感谢信
发表于 2024-11-26 15:25:57
SELECT p.name AS product_name, SUM(o.quantity) AS total_sales, RANK() OVER (PARTITION BY p.category ORDER BY SUM(o.quantity) DESC, p.produ 展开全文
头像 Jecyyy
发表于 2024-09-15 15:53:55
select product_name, total_sales, rank() over (partition by cate order by total_sales desc) as category_rank from (select a.name as prod 展开全文
头像 zzzzzz123_
发表于 2025-02-13 00:22:21
with tmp as( select sum(quantity) total_sales, category, name product_name, p.product_id id from products p j 展开全文
头像 今天也要早睡1
发表于 2025-06-10 16:24:19
select product_name,total_sales,row_number() over(partition by category order by total_sales desc) as category_rank from (select products.category ,pr 展开全文
头像 牛客479560235号
发表于 2025-03-25 17:07:18
SELECT t4.name product_name, t3.total_sales, t3.category_rank FROM ( SELECT t1.product_id, SUM(t2.quantity) total_sales, DENSE_RANK( 展开全文
头像 牛客804125561号
发表于 2024-09-26 17:24:51
select name as product_name, sum(quantity) as total_sales, rank() over (partition by category order by sum(quantity) desc) as category_rank from produ 展开全文
头像 MT2022
发表于 2025-03-29 23:04:38
以下是满足需求的 SQL 查询及详细解释: SELECT p.name AS product_name, SUM(o.quantity) AS total_sales, ROW_NUMBER() OVER ( PARTITION BY p.category ORDER B 展开全文