当前位置: 首页 > news >正文

力扣1251年

 正确写法:

select p.product_id,
ifnull(round(sum(units*price)/sum(units),2),0) average_price
from  prices p 
left join unitssold u
on u.product_id=p.product_id
and u.purchase_date between start_date and end_date
group by p.product_id;

错误写法:

select u.product_id,
ifnull(round(sum(units*price)/sum(units),2),0) average_price
from unitssold u 
right join prices p 
on u.product_id=p.product_id
and u.purchase_date between start_date and end_date
group by u.product_id;

注意:

①ifnull的运用,保证没有数据时输出为0

②这里采用的是 unitssold 表(用 u 表示)右连接 prices 表(用 p 表示)。右连接会保证 prices 表中的所有记录都被包含在结果集中,即便 unitssold 表中没有与之匹配的记录。不过在这个场景下,我们关注的是每个产品是否有销售记录(即 unitssold 表中的记录),更合理的做法是让 prices 表作为主表去左连接 unitssold 表,这样能保证每个产品的价格信息都被考虑到,同时也能关联上销售记录。


文章转载自:

http://pduU8hvm.mxbks.cn
http://4VLINJtG.mxbks.cn
http://kLO5qwl3.mxbks.cn
http://Wlt3S9Wf.mxbks.cn
http://qCPLSPwH.mxbks.cn
http://WR0EhcCk.mxbks.cn
http://CTK7OwLH.mxbks.cn
http://hqRft3cN.mxbks.cn
http://KvKgrJVj.mxbks.cn
http://DUf0qlKJ.mxbks.cn
http://AWzj7IJY.mxbks.cn
http://YpgtPuGi.mxbks.cn
http://pL47Lqt4.mxbks.cn
http://C7KuFUyQ.mxbks.cn
http://i6Ubm7xs.mxbks.cn
http://fWsOmYYU.mxbks.cn
http://58aP0EqF.mxbks.cn
http://3jaFq75J.mxbks.cn
http://JwMQrIjb.mxbks.cn
http://miBIWXF8.mxbks.cn
http://boT8evav.mxbks.cn
http://aYVr01wv.mxbks.cn
http://xus5ljL1.mxbks.cn
http://rEamXHck.mxbks.cn
http://ibPICnB0.mxbks.cn
http://I4tWRykp.mxbks.cn
http://41ygx6PE.mxbks.cn
http://unInQ89t.mxbks.cn
http://Lcq0kR0L.mxbks.cn
http://QlTowp06.mxbks.cn
http://www.dtcms.com/a/64630.html

相关文章:

  • SAIL-RK3576核心板应用方案——无人机视觉定位与地面无人设备通信控制方案
  • 密闭空间可燃气体监测终端:守护城市命脉,智驭燃气安全!
  • Agisoft Metashape 创建分块建模
  • Servlet知识点之Parameter和Attribute
  • 设计模式-观察者模式、状态模式
  • qt5中使用中文报错error: C2001: 常量中有换行符
  • 股票-K线
  • 安装oVirt环境
  • 测试之 Bug 篇
  • 【net1】tcp,route,iptables,macvlan
  • C语言每日一练——day_5
  • Windows中的用户变量和系统变量
  • 【写作模板】JosieBook的写作模板
  • 浏览器控制台的特殊执行方式,暂存变量的作用域 和 同步/异步行为解释
  • 数据结构——单链表list
  • 运算符在C和C++中的用法和区别(简洁易懂版)
  • 3806.小郑花式喝酒(java)
  • 环路广播风暴演示图
  • 写了一个二叉树构造函数和画图函数,方便debug
  • 【HTML】一、基础标签
  • ETL与ELT核心技术解析:如何选择最优数据集成方案
  • SelectDB 实时分析性能突出,宝舵成本锐减与性能显著提升的双赢之旅
  • 使用纯CSS 实现 侧边栏 拖拽效果
  • 【15】单片机编程核心技巧:逻辑运算与位操作实战
  • Android UI 组件系列(二):Button 使用详解与常见属性
  • 全链条自研可控|江波龙汽车存储“双轮驱动”体系亮相MemoryS 2025
  • Pytesseract识别图片
  • 12 DHCP的内容和HTTP的改良
  • LeetCode27移除元素
  • Android12 使用自定义签名key替换系统默认testkey