41.确定最佳顾客的另一种方式(二) OrderItems表代表订单信息,确定最佳顾客的另一种方式是看他们花了多少钱,OrderItems表有订单号order_num和item_price商品售出价格、quantity商品数量 order_num item_price quantity a1 10 105 a2 1 1100 a2 1 200 a4 2 1121 a5 5 10 a2 1 19 a7 7 5 Orders表含有字段order_num 订单号、cust_id顾客id order_num cust_id a1 cust 10 a2 cust 1 a3 cust2 a4 cust2 2 a5 cust221 a7 cust221 7 顾客表Customers有字段cust_id 客户id、cust_name 客户姓名 cust_id cust_name cust 10 andy cust 1 ben cust2 tony cust2 2 tom cust221 an cust221 7 hex 【问题】编写 SQL 语句,返回订单总价不小于1000 的客户名称和总额 (OrderItems 表中的order_num)。 提示:需要计算总和(item_price 乘以 quantity)。按总额对结果进行排序,请使用INNER JOIN 语法。 【示例结果】 cust_name total_price andy 1050 ben 1319 tom 2242 【示例解析】 总额(item_price 乘以 quantity)大于等于1000的订单号,例如a2对应的顾客id为cust1,cust1的顾客名称cust_name是ben,最后返回ben作为order_num a2的quantity * item_price总和的结果1319。

区块链毕设网qklbishe.com为您提供问题的解答

41.确定最佳顾客的另一种方式(二)

OrderItems表代表订单信息,确定最佳顾客的另一种方式是看他们花了多少钱,OrderItems表有订单号order_num和item_price商品售出价格、quantity商品数量
order_num item_price quantity
a1 10 105
a2 1 1100
a2 1 200
a4 2 1121
a5 5 10
a2 1 19
a7 7 5
Orders表含有字段order_num 订单号、cust_id顾客id
order_num cust_id
a1 cust10
a2 cust1
a3 cust2
a4 cust22
a5 cust221
a7 cust2217
顾客表Customers有字段cust_id 客户id、cust_name 客户姓名
cust_id cust_name
cust10 andy
cust1 ben
cust2 tony
cust22 tom
cust221 an
cust2217 hex
【问题】编写 SQL 语句,返回订单总价不小于1000 的客户名称和总额(OrderItems 表中的order_num)。
提示:需要计算总和(item_price 乘以 quantity)。按总额对结果进行排序,请使用INNER JOIN 语法。
【示例结果】
cust_name total_price
andy 1050
ben 1319
tom 2242
【示例解析】
总额(item_price 乘以 quantity)大于等于1000的订单号,例如a2对应的顾客id为cust1,cust1的顾客名称cust_name是ben,最后返回ben作为order_num a2的quantity * item_price总和的结果1319。

这段代码有一些问题,且不说格式的问题。

1)having的执行顺序在select之前,不能使用select中定义的别名;
2)三张表使用的隐式连接,直接from a, b, c 然后在where中作关联的限制,这种可读性较差,还是建议用join。
我的建议:

Select c.cust_name
,sum(a.item_price*a.quantity) as total_price
From OrderItems a
Inner join
Orders b
On a.order_num = b.order_num
Inner join Customers c
On b.cust_id = c.cust_id
Group by c.cust_name
Having sum(a.item_price*a.quantity) >=1000
Order by total_price
;

01:57

以上就是关于问题41.确定最佳顾客的另一种方式(二)
OrderItems表代表订单信息,确定最佳顾客的另一种方式是看他们花了多少钱,OrderItems表有订单号order_num和item_price商品售出价格、quantity商品数量 order_num item_price quantity a1 10 105 a2
1 1100 a2
1 200
a4
2 1121 a5
5 10 a2
1 19 a7 7 5 Orders表含有字段order_num 订单号、cust_id顾客id order_num cust_id a1 cust 10 a2
cust 1 a3 cust2
a4
cust2 2 a5
cust221 a7 cust221 7 顾客表Customers有字段cust_id 客户id、cust_name 客户姓名 cust_id cust_name cust 10 andy cust 1 ben cust2
tony cust2 2 tom cust221 an cust221 7 hex
【问题】编写 SQL 语句,返回订单总价不小于1000 的客户名称和总额 (OrderItems 表中的order_num)。 提示:需要计算总和(item_price 乘以 quantity)。按总额对结果进行排序,请使用INNER JOIN 语法。 【示例结果】 cust_name total_price andy 1050 ben
1319
tom
2242
【示例解析】 总额(item_price 乘以 quantity)大于等于1000的订单号,例如a2对应的顾客id为cust1,cust1的顾客名称cust_name是ben,最后返回ben作为order_num a2的quantity * item_price总和的结果1319。的答案

欢迎关注区块链毕设网-
web3一级市场套利打新赚钱空投教程

区块链NFT链游项目方科学家脚本开发培训

从业7年-专注一级市场


微信:btc9767
TELEGRAM :https://t.me/btcok9

具体资料介绍

web3的一级市场千万收益的逻辑


进群点我



qklbishe.com区块链毕设代做网专注|以太坊fabric-计算机|java|毕业设计|代做平台-javagopython毕设 » 41.确定最佳顾客的另一种方式(二) OrderItems表代表订单信息,确定最佳顾客的另一种方式是看他们花了多少钱,OrderItems表有订单号order_num和item_price商品售出价格、quantity商品数量 order_num item_price quantity a1 10 105 a2 1 1100 a2 1 200 a4 2 1121 a5 5 10 a2 1 19 a7 7 5 Orders表含有字段order_num 订单号、cust_id顾客id order_num cust_id a1 cust 10 a2 cust 1 a3 cust2 a4 cust2 2 a5 cust221 a7 cust221 7 顾客表Customers有字段cust_id 客户id、cust_name 客户姓名 cust_id cust_name cust 10 andy cust 1 ben cust2 tony cust2 2 tom cust221 an cust221 7 hex 【问题】编写 SQL 语句,返回订单总价不小于1000 的客户名称和总额 (OrderItems 表中的order_num)。 提示:需要计算总和(item_price 乘以 quantity)。按总额对结果进行排序,请使用INNER JOIN 语法。 【示例结果】 cust_name total_price andy 1050 ben 1319 tom 2242 【示例解析】 总额(item_price 乘以 quantity)大于等于1000的订单号,例如a2对应的顾客id为cust1,cust1的顾客名称cust_name是ben,最后返回ben作为order_num a2的quantity * item_price总和的结果1319。