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

oracle之分析函数解析及其应用场景的更多相关文章

  1. oracle的分析函数over 及开窗函数

    转:http://www.2cto.com/database/201310/249722.html oracle的分析函数over 及开窗函数   一:分析函数over   Oracle从8.1.6开 ...

  2. Oracle 10gR2分析函数

    Oracle 10gR2分析函数汇总 (Translated By caizhuoyi 2008‐9‐19) 说明:  1. 原文中底色为黄的部分翻译存在商榷之处,请大家踊跃提意见:  2. 原文中淡 ...

  3. Oracle开窗函数笔记及应用场景

    介绍Oracle的开窗函数之前先介绍一下分析函数,因为开窗函数也属于分析函数 分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返回多行,而聚合函数对于每个组只返回一行. 上面是 ...

  4. 超级牛皮的oracle的分析函数over(Partition by...) 及开窗函数 (转)

    http://zonghl8006.blog.163.com/blog/static/4528311520083995931317/ over(Partition by...) 一个超级牛皮的ORAC ...

  5. 超级牛皮的oracle的分析函数over(Partition by...) 及开窗函数

    over(Partition by...) 一个超级牛皮的ORACLE特有函数. 天天都用ORACLE,用了快2年了.最近才接触到这个功能强大而灵活的函数.真实惭愧啊! oracle的分析函数over ...

  6. Oracle 监听器日志解析

    Oracle监听器是驻留在Oracle实例所在服务器上的独立进程.作为客户端进程连接实例的重要沟通组件,Oracle监听器扮演着重要的地位.本篇将从监听器日志入手,分析阅读监听器日志和日常监听器常见行 ...

  7. oracle的分析函数over(Partition by...) 及开窗函数

        over(Partition by...) 一个超级牛皮的ORACLE特有函数. oracle的分析函数over 及开窗函数一:分析函数overOracle从8.1.6开始提供分析函数,分析函 ...

  8. Oracle SQL 硬解析和子游标

    Oracle SQL 硬解析和子游标 What reasons will be happening sql hard parse and generating new child cursors 在一 ...

  9. Oracle的分析函数

    Oracle的分析函数row_number(),rank(),dense_rank()的用法与区别 比如查询工资排名第7的员工信息,可以用分析函数来做. --查询工资排名第7的员工信息select * ...

随机推荐

  1. java将所有的字符串转换为大写或小写

    public class DaXie { public static void main(String[] args) { /**将所有的字符串转换成大写或小写字母并打印出来*/ String str ...

  2. Ajax实战(原生)

    /** 之前一直在学习ajax,也拿jQuery写了很多,感觉还是写原生的对基础的理论理解的深刻.特此书写. * 得到ajax对象 */ function getajaxHttp() { var xm ...

  3. ui自动化:python+appium----环境搭建

    前言: appium可以说是app最火的一个自动化框架,它的主要优势是支持android和ios,另外脚本支持java和python.以下为python+appium的安装教程... 环境准备... ...

  4. Alpha冲刺7

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10013652.html 作业博客:https://edu.cnblogs.com/campus ...

  5. asp.net mvc + dapper(ORM框架) + easyui框架简洁的信息管理项目

    1.目录结构: 2.效果图: 3.IndexController控制器: using System; using System.Collections; using System.Collection ...

  6. DAY 04运算符与流程控制

    输入输出补充: python2与python3的输入输出不同 python2中有两种用户 输入方式,一种是raw_input,和input raw_input与python3的input是相同的 而p ...

  7. U-Boot shell脚本语法

    /********************************************************************** * U-Boot shell脚本语法 * 说明: * 之 ...

  8. Android版本28使用http请求报错not permitted by network security policy

    Android版本28使用http请求报错not permitted by network security policy android模拟器调试登录的时候报错 CLEARTEXT communic ...

  9. Mad LIbs小游戏

    c1=input('请输入摄氏温度;') c2=float(c1)*9/5+32 print('摄氏温度转换成华氏温度是{}'.format(c2)) name1=input('请输入名字:') na ...

  10. addEventListener以及滑轮滑动事件的应用

    addEventListener用于向元素添加事件,而其适用于较新版的IE浏览器(如IE9),对于IE6/7/8来说,应该用attachEvent 下面的代码即为向<img>元素添加事件 ...