题解 | #返回每个顾客不同订单的总金额#

返回每个顾客不同订单的总金额

http://www.nowcoder.com/practice/ce313253a81c4947b20e801cd4da7894

#1
#内连接可以,题目原因左外连接不行。
SELECT cust_id,
       SUM(quantity * item_price) AS total_ordered
FROM OrderItems ot
JOIN Orders o ON ot.order_num = o.order_num  
# FROM Orders, OrderItems
# WHERE Orders.order_num = OrderItems.order_num
GROUP BY cust_id
ORDER BY total_ordered DESC;

第二种方法: #2

SELECT cust_id,
       (SELECT SUM(quantity * item_price)
        FROM OrderItems
        WHERE OrderItems.order_num = Orders.order_num) AS total_ordered
FROM Orders
ORDER BY total_ordered DESC;
全部评论

相关推荐

昨天 10:35
已编辑
西安科技大学 后端
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务