一、over()分析函数

  分组查前几条:select * from test t where (select count(*) from test a where t.type=a.type and t.scope>a.scope)<2;

  --rank()/dense_rank() over(partition by ...order by ...)
  select * from(select t.*,rank() over(partition by t.type order by t.scope ) a from TEST t) a  where a.a<3

--dense_rank()分级 连续排序
select t.*,dense_rank() over(partition by t.type order by t.scope)a from test t
--rank()分级 跳跃排序
select t.*,rank() over(partition by t.type order by t.scope)a from test t

select * from Test t where 2>(select count(*) from Test a where t.type=a.type and t.scope>a.scope)
select t.* from Test t,(select a.type,max(a.scope) scope from TEST a group by a.type) d  where t.type=d.type and t.scope=d.scope

--笛卡尔乘积
select * from Test t,Test a
select t.* from Test t,(select a.type,max(a.scope) maxscope,min(a.scope) minscope from TEST a group by a.type) d  where t.type=d.type and t.scope=d.scope

  --
select t.*,d.maxscope-t.scope maxscope,t.scope-d.minscope minscope
  from Test t,
       (select a.type, max(a.scope) maxscope, min(a.scope) minscope
          from TEST a
         group by a.type) d
 where t.type = d.type

--min()/max() over(partition by ...)
select t.*,
       nvl(max(t.scope) over(partition by t.type), 0) - t.scope maxscope,
       t.scope - nvl(min(t.scope) over(partition by t.type), 0) minscope
  from test t

--lead()/lag() over(partition by ... order by ...)  
select t.*,lead(t.scope,1,0)over(partition by t.type order by t.scope) a--同组后一个
       from test t
select t.*,lag(t.scope,1,0)over(partition by t.type order by t.scope) a--同组前一个
       from test t

select t.*,
       first_value(t.scope) over(partition by t.type) first_sal,
       last_value(t.scope) over(partition by t.type) last_sal,
       sum(t.scope) over(partition by t.type) sum_sal,
       avg(t.scope) over(partition by t.type) avg_sal,
       count(t.scope) over(partition by t.type) count_num,
       row_number() over(partition by t.type order by t.scope) row_num
  from test t

--注:带order by子句的方法说明在使用该方法的时候必须要带order by

oracle复杂查询是sql的更多相关文章

  1. oracle下查询的sql已经超出IIS响应时间

    场景: 最近一直发生oracle下查询的sql已经超出IIS响应时间,但是后台DB的SQL查询还未终止,一直在查询.这对DB是造成很大的压力. 解决办法 增加OracleCommand 中的Comma ...

  2. oracle数据库查询日期sql语句(范例)、向已经建好的表格中添加一列属性并向该列添加数值、删除某一列的数据(一整列)

    先列上我的数据库表格: c_date(Date格式)     date_type(String格式) 2011-01-01                   0 2012-03-07         ...

  3. Oracle top 查询TOP SQL

    有时Oracle数据库服务器,系统CPU爆高,通过Top命令可以查看到占用CPU最高的进程 我们需要记住前几个TOP的pid号,带入下面的SQL,到数据库中查询运行的进程.服务器.用户.SQL.等待等 ...

  4. Oracle分页查询和SQL server分页查询总结

    分页查询是项目中必不可少的一部分,难倒是不难,就是这些东西,长时间不用,就忘的一干二净了.今天特此总结一下这两款数据库分页查询的实现过程(只记录效率比较高的) 一.Oracle中的分页查询 1.通用分 ...

  5. oracle数据库查询时间sql

    select * from cc_picture_info where PICTURE_SOURCE = 3 AND UPLOAD_TIME > to_date('2017-03-29 16:5 ...

  6. 整理oracle 树形查询

    注:本文参考了<整理oracle 树形查询> sql树形递归查询是数据库查询的一种特殊情形,也是组织结构.行政区划查询的一种最常用的的情形之一.下面对该种查询进行一些总结: create ...

  7. 【SQL】Oracle分页查询的三种方法

    [SQL]Oracle分页查询的三种方法 采用伪列 rownum 查询前10条记录 ? 1 2 3 4 5 6 7 8 9 10 11 [sql] select * from t_user t whe ...

  8. 查询Oracle正在执行的sql语句

    --查询Oracle正在执行的sql语句及执行该语句的用户 SELECT b.sid oracleID, b.username 登录Oracle用户名, b.serial#, spid 操作系统ID, ...

  9. ORACLE PATCH 版本的查询 PL/SQL

    --ORACLE PATCH 版本的查询 PL/SQL SELECT DD.PATCH_NAME,        PP.CREATION_DATE,        PP.DRIVER_FILE_NAM ...

随机推荐

  1. Mysql_Learning_Notes_系统结构_1_数据类型

    数据类型 整型 1.tinyint 1Bytes -128~127(255) 2.smallint 2Bytes -32768~32676(65535) 3.mdeiumint 3Bytes -838 ...

  2. nginx配置location总结及rewrite规则写法【转】

    转自 nginx配置location总结及rewrite规则写法 | Sean's Noteshttp://seanlook.com/2015/05/17/nginx-location-rewrite ...

  3. CentOS7 修改网卡名称

    vi /etc/sysconfig/grub 增加net.ifnames=0 biosdevname=0 执行:grub2-mkconfig -o /boot/grub2/grub.cfg

  4. 【oracle】入门学习(二)

    oracle登录身份有三种:normal 普通身份sysdba 系统管理员身份sysoper 系统操作员身份每种身份对应不同的权限 sysdba权限:●启动和关闭操作●更改数据库状态为打开/装载/备份 ...

  5. python 图片上传写入磁盘功能

    本文是采取django框架,前端上传图片后端接收后写入磁盘,数据库记录图片在磁盘上的路径(相对),以下是前端上传到后端入库的基本流程 一. html代码 <!DOCTYPE html> & ...

  6. XML文件解析-SaxReader

    一.前言 java解析xml文件有几种方式,这里介绍一下用SaxReader来解析Xml的方法. 二.准备 如果用SaxReader的话,需要引入jar包dom4j, 版本的话官网下载一个就好,这里用 ...

  7. 【笔记】Python简明教程

    Python简明教程,此资源位于http://woodpecker.org.cn/abyteofpython_cn/chinese/ s=u'中文字符' #u表示unicode,使用u之后能正常显示中 ...

  8. 配置toad远程连接oracle

    在oracle服务器上: C:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN目录 文件:listener.ora(10.144.118 ...

  9. IIS部署asp.net MVC 出现错误 403.14-Forbidden解决办法

    可能性一: <system.webServer>   <validationvalidateIntegratedModeConfiguration="false" ...

  10. SQL group 分组查询

    1.使用group by进行分组查询  在使用group by关键字时,在select列表中可以指定的项目是有限制的,select语句中仅许以下几项:  被分组的列 为每个分组返回一个值得表达式,例如 ...