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. Linux c 从文件当中读取任意一行的数据

    代码如下 #include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <sys/ty ...

  2. [Codeforces771E]Bear and Rectangle Strips

    Problem 给你一个2*n的矩阵,要求你用补充叠的矩阵去框,要求每个矩阵框中的数之和为0,问最多可以用几个矩阵. Solution 首先预处理出一个点到离它最近的一段和为0的区间的左端点 然后到这 ...

  3. flashfxp 数据socket错误 连接已超时 filezilla

    最近windows server 开启了防火墙后发现flashfxp连不上,报超时. 1,服务端的动态端口从指定的范围内取, 2,防火墙开启范围内端口. 参考:http://jingyan.baidu ...

  4. 初见《构建之法》orz……

    作为一个大二的计科狗,还是对软件设计有些懵,尽管之前学习过软件工程,但并没有掌握,突如其来的软件设计实践,让我着实傻眼. 通过消息得知,要学习<构建之法>,就去搜了一下,它是以实践为基础的 ...

  5. 浅谈URI和URL

    URI(Uniform Resource Identifier)字面上的意思是,统一资源标示符 URL(Uniform Resource Locator),统一资源定位符 光从字面上的意思,这个2个东 ...

  6. 剑指Offer 55. 链表中环的入口结点 (链表)

    题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目地址 https://www.nowcoder.com/practice/253d2c59ec3e4bc68d ...

  7. 复习-java-001

    一.基本数据类型: byte.boolean.char.string.int.short.long.float.double 二.java三大特性: 1.封装 封装是实现面向对象程序设计的第一步,封装 ...

  8. spring jpa Pageable 分页之---多条件排序

    Sort sort = new Sort(Direction.ASC, "sort").and(new Sort(Direction.DESC, groupField));//排序 ...

  9. PHP输出缓存ob系列函数详解

    PHP输出缓存ob系列函数详解 ob,输出缓冲区,是output buffering的简称,而不是output cache.ob用对了,是能对速度有一定的帮助,但是盲目的加上ob函数,只会增加CPU额 ...

  10. docker 常见错误总结

    docker common error Non-existing image of running container drm() { docker rm $(docker ps -q -a); } ...