diff --git a/数据库系统原理与实践/平时作业/第三次作业.tex b/数据库系统原理与实践/平时作业/第三次作业.tex index fff5cd2..fe19d51 100644 --- a/数据库系统原理与实践/平时作业/第三次作业.tex +++ b/数据库系统原理与实践/平时作业/第三次作业.tex @@ -322,21 +322,19 @@ select cust_id, order_date from Orders natural join OrderItems where prod_id='BR \end{csv} \end{verification} \questionandanswer[]{ - 有一个顾客ID列表,其中包含他们已订购的总金额。OrderItems 表代表订单信息,OrderItems 表有订单号:order_num 和商品售出价格:item_price、商品数量:quantity。编写SQL语句,返回顾客 ID(Orders 表中的 cust_id),并使用子查询返回 total_ordered 每个顾客的订单总数,将结果按金额从大到小排序。 + 有一个顾客ID列表,其中包含他们已订购的总金额。OrderItems 表代表订单信息,OrderItems 表有订单号:order_num 和商品售出价格:item_price、商品数量:quantity。编写SQL语句,返回顾客 ID(Orders 表中的 cust_id),并使用子查询返回 total_ordered 每个顾客的所有订单总金额,将结果按金额从大到小排序。 \includegraphics[width=0.5\linewidth]{imgs/2024-10-04-10-27-32.png} - }{ - 注意返回的是订单总数,但是要按照金额从大到小排序。 - } + }{} { \begin{minted}{SQL} -select cust_id, count(*) as total_ordered +select cust_id, sum(total) as total_ordered from Orders join (select order_num, sum(item_price * quantity) as total from OrderItems group by order_num) as a on Orders.order_num = a.order_num group by cust_id -order by sum(total) desc; +order by total_ordered desc; \end{minted} } \begin{verification} @@ -368,19 +366,19 @@ values ('a0001', 'cust10'), ('a0003', 'cust1'), ('a0013', 'cust2'); -select cust_id, count(*) as total_ordered +select cust_id, sum(total) as total_ordered from Orders join (select order_num, sum(item_price * quantity) as total from OrderItems group by order_num) as a on Orders.order_num = a.order_num group by cust_id -order by sum(total) desc; +order by total_ordered desc; \end{minted} \begin{csv} ,cust_id,total_ordered -1,cust2,1 -2,cust1,2 -3,cust10,1 +1,cust2,2242 +2,cust1,1404 +3,cust10,1050 \end{csv} \end{verification} \questionandanswer[]{