分析

  • 统计总金额。round(sum(item_price*quantity)) as total_orderd
  • select中建立两表的联系。where t1.order_num=Orders.order_num
  • 从表Oders中选出cust_id,total_ordered

代码

select cust_id,
    (select total_orderd
    from
        (select order_num,round(sum(item_price*quantity)) as total_orderd
        from OrderItems
        group by order_num
         )as t1
    where t1.order_num=Orders.order_num
     ) as total_ordered
from Orders
order by total_ordered desc