题解 | #返回每个顾客不同订单的总金额#
返回每个顾客不同订单的总金额
https://www.nowcoder.com/practice/ce313253a81c4947b20e801cd4da7894
用WITH...AS创建新表total_order_price,然后通过键order_num将其与Orders表相连
WITH total_order_price AS (
SELECT order_num, SUM(item_price*quantity) AS total_ordered
FROM OrderItems
GROUP BY order_num
)
SELECT cust_id, total_ordered
FROM Orders
LEFT JOIN
total_order_price
USING(order_num)
ORDER BY total_ordered DESC
#sql#SELECT order_num, SUM(item_price*quantity) AS total_ordered
FROM OrderItems
GROUP BY order_num
)
SELECT cust_id, total_ordered
FROM Orders
LEFT JOIN
total_order_price
USING(order_num)
ORDER BY total_ordered DESC