电商系统,数据库中有以下表:
orders
表:存储订单信息。order_items
表:存储订单中的商品信息。products
表:存储商品信息。表结构如下:
-- 订单表
CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
order_date DATE,
customer_id INT
);
-- 商品表
CREATE TABLE products (
product_id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(50),
category VARCHAR(50),
price DECIMAL(10, 2)
);
-- 订单商品表
CREATE TABLE order_items (
order_item_id INT AUTO_INCREMENT PRIMARY KEY,
order_id INT,
product_id INT,
quantity INT,
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
插入测试数据:
-- 插入订单数据
INSERT INTO orders (order_date, customer_id) VALUES
('2023-10-01', 1),
('2023-10-02', 2),
('2023-10-03', 1),
('2023-10-04', 3),
('2023-10-05', 2);
-- 插入商品数据
INSERT INTO products (product_name, category, price) VALUES
('Laptop', 'Electronics', 1200.00),
('Smartphone', 'Electronics', 800.00),
('Headphones', 'Electronics', 150.00),
('Coffee Maker', 'Home Appliances', 100.00),
('Blender', 'Home Appliances', 80.00);
-- 插入订单商品数据
INSERT INTO order_items (order_id, product_id, quantity) VALUES
(1, 1, 1), -- Laptop
(1, 3, 2), -- Headphones
(2, 2, 1), -- Smartphone
(3, 4, 1), -- Coffee Maker
(4, 5, 1), -- Blender
(5, 1, 1), -- Laptop
(5, 2, 1); -- Smartphone
请编写一个 SQL 查询,实现以下需求:
customer_id
total_spent
(该客户的总消费金额)most_expensive_order
(该客户最贵的一笔订单的金额)favorite_category
(该客户购买次数最多的商品类别)total_spent
从高到低排序。上述数据,查询结果应该类似:
customer_id | total_spent | most_expensive_order | favorite_category |
---|---|---|---|
1 | 1700.00 | 1500.00 | Electronics |
2 | 2080.00 | 2000.00 | Electronics |
orders
表和 order_items
表关联,计算每个订单的总金额,再按客户分组求和。MAX()
聚合函数和子查询,计算每个客户最贵的一笔订单的金额。COUNT()
和 GROUP BY
统计每个客户购买的商品类别次数,再使用窗口函数 ROW_NUMBER()
或 RANK()
找到购买次数最多的类别。HAVING
条件过滤总消费金额超过 1000 的客户。-- 第一步:计算每个客户的总消费金额和最贵的一笔订单金额
WITH customer_spending AS (
SELECT
o.customer_id,
SUM(oi.quantity * p.price) AS total_spent,
MAX(oi.quantity * p.price) AS most_expensive_order
FROM
orders o
JOIN order_items oi ON o.order_id = oi.order_id
JOIN products p ON oi.product_id = p.product_id
GROUP BY
o.customer_id
HAVING
total_spent > 1000
),
-- 第二步:计算每个客户购买次数最多的商品类别
favorite_category AS (
SELECT
o.customer_id,
p.category,
COUNT(*) AS category_count,
ROW_NUMBER() OVER (PARTITION BY o.customer_id ORDER BY COUNT(*) DESC) AS category_rank
FROM
orders o
JOIN order_items oi ON o.order_id = oi.order_id
JOIN products p ON oi.product_id = p.product_id
GROUP BY
o.customer_id, p.category
)
-- 第三步:整合结果并排序
SELECT
cs.customer_id,
cs.total_spent,
cs.most_expensive_order,
fc.category AS favorite_category
FROM
customer_spending cs
JOIN favorite_category fc ON cs.customer_id = fc.customer_id
WHERE
fc.category_rank = 1
ORDER BY
cs.total_spent DESC;
SUM()
计算总消费金额,使用 MAX()
计算最贵的一笔订单金额,使用 COUNT()
统计购买次数。ROW_NUMBER()
计算每个客户购买次数最多的商品类别。WITH
子句将复杂查询分解为多个步骤,提高可读性。扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有