题解 | #返回每个顾客不同订单的总金额#
返回每个顾客不同订单的总金额
https://www.nowcoder.com/practice/ce313253a81c4947b20e801cd4da7894
-- 必须联表查询 select cust_id,total_ordered from Orders o join ( select order_num,sum(item_price *quantity) as total_ordered from OrderItems group by order_num ) t on o.order_num = t.order_num order by total_ordered desc;
必须经过联表查询,才能筛选出结果。
使用聚合函数sum,必须进行分组group by。注意:聚合函数一般在select后面,这样想,就不会出错了。
子查询筛选的时候,记得给sum的结果命名,否则外层查询找不到total_ordered。