oracle查询返回数量,Oracle查询按年累计数量

您需要两层分析函数:您需要解析MAX(,其值为),因为您要返回原始表中的所有行;而在MAX内,您需要分析(滚动)平均值。分析函数不能嵌套,所以你需要一个子查询和一个外部查询。这样的事情:

with inputs (item_id, org_id, yr, qty) as (

select 100, 121, 2015, 10 from dual

union all select 100, 121, 2016, 5 from dual

union all select 100, 121, 2017, 8 from dual

union all select 101, 146, 2014, 10 from dual

union all select 101, 146, 2015, 11 from dual

union all select 101, 146, 2016, 12 from dual

union all select 101, 146, 2017, 13 from dual

)

-- End of simulated inputs (for testing only, not part of the solution).

-- SQL query begins BELOW THIS LINE. Use your actual table and column names.

select item_id, org_id, yr, qty,

max(forward_avg) over (partition by item_id, org_id ) as max_avg

from (select item_id, org_id, yr, qty,

avg(qty) over (partition by item_id, org_id

order by yr desc ) as forward_avg

from inputs i

) b

order by item_id, org_id, yr -- If needed

;

ITEM_ID ORG_ID YR QTY MAX_AVG

---------- ---------- ---------- ---------- ----------

100 121 2015 10 8

100 121 2016 5 8

100 121 2017 8 8

101 146 2014 10 13

101 146 2015 11 13

101 146 2016 12 13

101 146 2017 13 13

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值