数据汇总问题

mqmmx 2006-12-31 01:07:52
数据表内容如下
id location qty
a 1 100
a 2 50
b 2 100
c 1 200
a 3 100
......

现我要得到id = 'a' 数量(qty)为150(此数量可变)的内容明细如何写语句

结果为
id location qty
a 1 100
a 2 50

不用循环如何写
...全文
185 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
armorking2003 2007-01-01
  • 打赏
  • 举报
回复
select * from (
select id, location,qty,
sum(qty) over (PARTITION BY id, location) snum --连续求和, 按照OVER后边内容汇总求和
from tab
where id='a'
)
where snum=150
wiler 2006-12-31
  • 打赏
  • 举报
回复
select * from (
select id, location,qty,
sum(qty) over (order by id, location) snum --连续求和, 按照OVER后边内容汇总求和
from tab
where id='a'
)
where snum=150
mqmmx 2006-12-31
  • 打赏
  • 举报
回复
qty=150是汇总数,可能不是150这么巧,
可能只是180那样结果就会变成
id location qty
a 1 100
a 3 100
两个中任何一个100中得个80和前一个相加就行了


上面表中的内容是库存,要求的qty是需要出库的数量,我要得到的明细是>=出库数量的记录明细,当然大于的话只能是多一条记录而已,
可能有几十万条记录满足条件,我要求的是那个正好满足条件或大于一条记录的汇总明细



jacalwu 2006-12-31
  • 打赏
  • 举报
回复
嘻嘻 ,理解错了,搂上的sql 才对
jacalwu 2006-12-31
  • 打赏
  • 举报
回复
select id,location ,sum(qty)
from tab
where id='a'
group by id,location
having sum(qty)=150
tgm78 2006-12-31
  • 打赏
  • 举报
回复
select * from tab where in (
select id from tab group by id having sum(qty)=150 and id='a')
realx 2006-12-31
  • 打赏
  • 举报
回复
select * from tab where id in (
select id from tab group by id having sum(qty)=150 and id='a')
caixia615 2006-12-31
  • 打赏
  • 举报
回复
题意说的不清...
select * from tablename where id='a' and qty=150 ???

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧