From 4a4d9e0e21fb06fed9bea3f44188dd6b79366f6c Mon Sep 17 00:00:00 2001 From: 423A35C7 <609514299@qq.com> Date: Thu, 10 Oct 2024 16:48:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E5=8E=9F=E7=90=86=E4=B8=8E=E5=AE=9E=E8=B7=B5=E2=80=94=E2=80=94?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 题目改了,之前应该是出错了。按照正常情况就应该是返回订单总金额。 --- 数据库系统原理与实践/平时作业/第三次作业.tex | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) 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[]{