题解 | #返回每个顾客不同订单的总金额#
返回每个顾客不同订单的总金额
https://www.nowcoder.com/practice/ce313253a81c4947b20e801cd4da7894
首先发现需要返回cust_id和total_order。而total_order是需要使用函数计算的。
故,
- 主查询从Orders表中进行查询得到cust_id和order_num
- 子查询针对主查询中得到的每一条记录,去OrderItems表中找,与order_num相等的行。
- 子查询中计算结果行,得到total_ordered。
SELECT
cust_id,
(
SELECT
SUM(item_price * quantity)
FROM
OrderItems
WHERE
OrderItems.order_num = Orders.order_num
) total_ordered
FROM
Orders
ORDER BY
total_ordered DESC;


