over partition by order by rows|range(4)之first_value和last_value

在通常的max和min聚合函数中,可以取得分区中的最大值和最小值。

但是,如果取得最大值或者最小值所在的行的其它列。,就比较困难了。

因此,oracle提供了First_value和Last_value两个分析函数,可以得到各个列的值。

语法如下:

first_value(expression) 

over (

partition by ... order by ... rows between ...

)

Last_value(expression) 

over (

partition by ... order by ... rows between ...

)

通过对需要的列进行排序,可以达到指定行的数据。

示例如下:

--first_value
select year,week,product,
sale,
first_value(sale) over ( --读取排序以后的第一行(最大值)
partition by product,region,country,year
order by sale desc
rows between unbounded preceding and unbounded following
) top_sale,
first_value(week) over (--读取排序以后的第一行(week值)
partition by product,region,country,year
order by sale desc
rows between unbounded preceding and unbounded following
) top_week
from sales_fact
where product='product1' and country='country1' and region='region1'
order by product,country,year,week

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值