一、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. 关于Linux内核版本

    Linux内核可分为实验版本和产品化版本.每一个版本号由三位数字“x.y.z”组成,第二位数字说明版本类型:偶数表示产品化版本,奇数表示实验版本.产品化版本只修改错误,而实验版本最初是产品化版本的拷贝 ...

  2. Linux下C程序的反汇编【转】

    转自:http://blog.csdn.net/u011192270/article/details/50224267 前言:本文主要介绍几种反汇编的方法. gcc gcc的完整编译过程大致为:预处理 ...

  3. 使用postman做接口测试(三)

    三,接口用例的设计 个人感觉用例的设计才是重要的哈,网上查了一些资料总结了一下 1.业务流程测试 通过性验证: 1, 按照接口文档上的参数,正常传参,是否可以返回正确的结果 2, 是否满足前提条件,比 ...

  4. 一文看懂python主要应用领域或应用场景

    Python简介 Python(英国发音:/ˈpaɪθən/美国发音:/ˈpaɪθɑːn/),是一种面向对象的解释型计算机程序设计语言,由荷兰人GuidovanRossum于1989年发明,第一个公开 ...

  5. NLP里面好的学习资料

    别人推荐的网址: http://ruder.io/deep-learning-nlp-best-practices/index.html#wordembeddings

  6. 洛谷P1120 小木棍(升级版)

    传送门啦 一道经典的搜索剪枝题,不废话,步入正题. 分析: 一.输入时手动过滤不合法的情况 二.很明显我们要枚举把哪些棍子拼接成原来的长棍,而原始长度(原来的长棍的长度)都相等,因此我们可以在 $ d ...

  7. python 多线程删除MySQL表

    一.需求分析 在<python 统计MySQL表信息>这篇博客中,链接如下: https://www.cnblogs.com/xiao987334176/p/9901692.html 已经 ...

  8. Linux学习笔记:rm删除文件和文件夹

    使用rm命令删除一个文件或者目录 使用rmdir可以删除空文件夹 参数: -i:删除前逐一询问确认 -f:即使原档案属性设为唯读,亦直接删除,无需逐一确认 -r:递归 删除文件可以直接使用rm命令,若 ...

  9. Jquery Datatable添加复选框,实现批量操作。

    最近一段时间,一直在写前端的东西,自己也不擅长,最近也有所长进,把工作中用到的一些前端知识整理一下,下次用到就不用再找了.这次主要是在datatable中添加复选框,然后实现批量操作的功能.因为是公司 ...

  10. Django和Mysql合用时,显示时间问题

    这个以前没系统处理过,感觉前端页面显示正常,就OK. 但有的不重要的地方,显示有8小时错乱,也没有列入优先级处理. 昨天下细看了一些网上文档,找取了解决思路. 大致想法是:数据库里存+00:00时区的 ...