【题目要求】 假设你有两个表:orders 表是订单信息表和 customer表是顾客信息表 orders 表包含以下字段: order_id:订单的唯一标识符。 customer_id:顾客的唯一标识符。 order_date:订单的日期。 total_amount:订单的总金额。 customers 表包含以下字段: customer_id:顾客的唯一标识符。 customer_name:顾客的姓名。 city:顾客所在的城市。 请你编写一个SQL查询语句,查询每个城市的用户的订单总金额,并按照总金额的降序进行排序(当金额出现一致的情况,按照城市的名称升序排列)。 【解释】 比如:New York城市,只有一个Alice顾客,该顾客共消费2笔分别是order_id是1和2的两个订单,总金额是100+200 = 300
示例1
输入
drop table if exists orders ;
CREATE TABLE orders (
order_id INT,
customer_id INT,
order_date DATE,
total_amount DECIMAL(10, 2)
);
drop table if exists customers ;
CREATE TABLE customers (
customer_id INT,
customer_name VARCHAR(50),
city VARCHAR(50)
);
INSERT INTO orders (order_id, customer_id, order_date, total_amount)
VALUES
(1, 1, '2023-01-01', 100.00),
(2, 1, '2023-02-01', 200.00),
(3, 2, '2023-03-01', 150.00),
(4, 2, '2023-04-01', 300.00),
(5, 3, '2023-05-01', 250.00);
INSERT INTO customers (customer_id, customer_name, city)
VALUES
(1, 'Alice', 'New York'),
(2, 'Bob', 'Los Angeles'),
(3, 'Catherine', 'Chicago');
输出
city|total_order_amount
Los Angeles|450.00
New York|300.00
Chicago|250.00
加载中...