oracle分析函数结构,oracle之分析函数解析及其应用场景

ORACLE 分析函数FIRST_VALUE,LAST_VALUE用法

sum over

avg over

first_value over

last_value over

...聚合函数结合over就是分析函数

备注LAST_VALUE一般这样操作:

【ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING / ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW】

3、列出每月的订单总额以及截至到当前月的订单总额

SQL> select month,

2         sum(tot_sales) month_sales,

3         sum(sum(tot_sales)) over(order by month

4           rows between unbounded preceding and current row) current_total_sales

5    from orders

6   group by month;

MONTH MONTH_SALES CURRENT_TOTAL_SALES

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

1      610697              610697

2      428676             1039373

3      637031             1676404

4      541146             2217550

5      592935             2810485

6      501485             3311970

7      606914             3918884

8      460520             4379404

9      392898             4772302

10      510117             5282419

11      532889             5815308

12      492458             6307766

SQL> select month,

2         sum(tot_sales) month_sales,

3         sum(sum(tot_sales)) over(order by month

4         rows between unbounded preceding and current row) current_total_sales,

5         sum(sum(tot_sales)) over(order by month

6         rows between unbounded preceding and unbounded following) total_sales

7    from orders

8   group by month;

MONTH MONTH_SALES CURRENT_TOTAL_SALES TOTAL_SALES

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

1      610697              610697     6307766

2      428676             1039373     6307766

3      637031             1676404     6307766

4      541146             2217550     6307766

5      592935             2810485     6307766

6      501485             3311970     6307766

7      606914             3918884     6307766

8      460520             4379404     6307766

9      392898             4772302     6307766

10      510117             5282419     6307766

11      532889             5815308     6307766

12      492458             6307766     6307766

4、统计当天销售额和五天内的评价销售额

select trunc(order_dt) day,

sum(sale_price) daily_sales,

avg(sum(sale_price)) over (order by trunc(order_dt)

range between interval '2' day preceding

and interval '2' day following) five_day_avg

from cust_order

where sale_price is not null

and order_dt between to_date('01-jul-2001','dd-mon-yyyy')

and to_date('31-jul-2001','dd-mon-yyyy')

为了对指定范围进行统计,Oracle使用关键字range、interval来指定一个范围。上面的例子告诉Oracle查找当前日期的前2天,后2天范围内的记录,并统计其销售平均值

5、Oracle提供了2个额外的函数:first_value、last_value,用于在窗口记录集中查找第一条记录和最后一条记录。假设我们的报表需要显示当前月、上一个月、后一个月的销售情况,以及每3个月的销售平均值,这两个函数就可以派上用场了

select month,

first_value(sum(tot_sales)) over (order by month

rows between 1 preceding and 1 following) prev_month,

sum(tot_sales) monthly_sales,

last_value(sum(tot_sales)) over (order by month

rows between 1 preceding and 1 following) next_month,

avg(sum(tot_sales)) over (order by month

rows between 1 preceding and 1 following) rolling_avg

from orders

where year = 2001

and region_id = 6

group by month

order by month;

首先我们来看:rows between 1 preceding and 1 following告诉Oracle在当前记录的前一条、后一条范围内查找并统计,而first_value和last_value在这3条记录中至分别找出第一条、第三条记录,这样我们就轻松地得到相邻三个月的销售记录及平均值了!

6、我们知道了如何利用窗口函数来显示相邻的记录,现在假如我们想每次显示当月的销售额和上个月的销售额,应该怎么做呢?

从第五点的介绍我们可以知道,利用first_value(sum(tot_sales) over (order by month rows between 1 preceding and 0 following))就可以做到了,其实Oracle还有一个更简单的方式让我们来比较2条记录,它就是lag函数。

leg函数类似于preceding和following

子句,它能够通过和当前记录的相对位置而被应用,在比较同一个相邻的记录集内两条相邻记录的时候特别有用。

select  month,

sum(tot_sales) monthly_sales,

lag(sum(tot_sales), 1) over (order by month) prev_month_sales

from orders

where year = 2001

and region_id = 6

group by month

order by month;

*************************************************

1、over函数的写法:

over(partition by class order by sroce) 按照sroce排序进行累计,order by是个默认的开窗函数,按照class分区。

2、开窗的窗口范围:

over(order by sroce range between 5 preceding and 5 following):窗口范围为当前行数据幅度减5加5后的范围内的。

over(order by sroce rows between 5 preceding and 5 following):窗口范围为当前行前后各移动5行。

3、与over()函数结合的函数的介绍

(1)、查询每个班的第一名的成绩:如下

1 SELECT * FROM (select t.name,t.class,t.sroce,rank() over(partition by t.class order by t.sroce desc) mm from T2_TEMP t) where mm = 1;

*************************************************

分析函数应用场景:https://blog.csdn.net/WuLex/article/details/82796991

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值