题解 | #确定最佳顾客的另一种方式(二)#

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

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

这个题目还可以。

  • 使用Orders表和顾客表Customers连接,可以得到每个订单对应得顾客姓名;
  • 使用OrderItems表和Orders表连接,可以得到每个订单对应得金额;
  • 然后使用group by进行分组,分组依据为顾客姓名;
  • 最后对每个顾客的订单金额进行求和,筛选,排序。
select c.cust_name, sum(oi.item_price * oi.quantity) as total_price
from Orders o join Customers c on o.cust_id=c.cust_id
join OrderItems oi on o.order_num=oi.order_num
group by c.cust_name
having total_price >= 1000
order by total_price;
  • 注意:从group by字段开始,就可以使用select子句里面的别名了哦。
全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞 回复 分享
发布于 2022-04-27 12:07

相关推荐

06-07 19:59
门头沟学院 C++
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
点赞 评论 收藏
分享
评论
4
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务