分析函数
文章平均质量分 54
jgmydsai
道森oracle讲师,课程:sql高级查询改写
展开
-
取各部门工资最高人员名称
SQL> select b.dname, a.ename, a.sal, max(a.ename) keep(dense_rank last order by a.sal) over(partition by b.dname) as no1, first_value(a.ename) over(partition by b.dname) as原创 2013-07-21 11:33:53 · 1927 阅读 · 0 评论 -
Oracle 11g r2分析函数新特性(一)分析函数NTH_VALUE
from http://tech.ddvip.com/2013-09/1378491397202257.html在11gr2中,Oracle分析函数的功能进一步增强。这篇介绍新增的分析函数NTH_VALUE。以前版本的分析函数,提供了FIRST_VALUE和LAST_VALUE的功能,而11gr2中,Oracle增加了一个NTH_VALUE的功能,这个功能包含了FIRS转载 2013-11-06 14:31:50 · 2011 阅读 · 1 评论 -
Oracle 11g r2分析函数新特性(三)分析函数LAG和LEAD的增强
from http://tech.ddvip.com/2013-09/1378491557202259.html在11gr2中,Oracle分析函数的功能进一步增强。这篇介绍分析函数LAG和LEAD的增强。11gr2对LAG和LEAD函数进行了增强,添加了IGNORE NULLS的功能。SQL> select * from v$version;BANNE转载 2013-11-06 14:36:55 · 1427 阅读 · 0 评论 -
一次分析函数的应用
下面是一个网友的问题问:表test(yyyymmdd,price)按yyyymmdd--yyyymmdd+3,sum(price)做统计,sql怎么写?例如:20131101--20131103 10020131102--20131104 10520131103--20131105 95于是生成环境及语句如下:SQL> DROP TABLE TEST原创 2013-11-07 20:09:57 · 922 阅读 · 0 评论 -
sql解惑谜题3 麻醉师谜题
SELECT B.ANEST_NAME, B.PROC_ID, B.START_TIME, B.END_TIME, MAX(CT) AS CT FROM (SELECT A.START_TIME AS LST_TIME, B.ANEST_NAME, B.PROC_ID, B.START_TIME,原创 2013-08-27 11:47:25 · 1341 阅读 · 1 评论 -
用分析函数优化标量子查询
以下语句表名及部分字段作过替换处理SELECT ii.*, CASE WHEN (SELECT COUNT(1) FROM ii ii WHERE ii.id > 0 AND ii.itemtranflag = 2原创 2013-11-12 14:48:29 · 1485 阅读 · 0 评论 -
比例函数
SQL> SQL> select sum(ct) as ct, sum(ratio_ct) as ratio_ct, object_type 2 from (select count(*) as ct, 3 round(ratio_to_report(count(*)) over() * 100, 2) as ratio_ct, 4原创 2013-07-20 21:34:45 · 950 阅读 · 0 评论 -
用分析函数去掉标量子查询
原语句如下(里面字段及表名称替换过的)SELECT A.CODE AS CODE, A.M_CODE AS M_CODE, A.STKTYPE AS F_STYPE, A.E_YEAR AS E_YEAR, B.SNAME AS SNAME, A.C_DATE AS C_DATE, TO_CHAR(SYSDAT原创 2014-03-04 13:55:13 · 1435 阅读 · 0 评论 -
MEDIAN CUME_DIST PERCENT_RANK PERCENTILE_CONT PERCENTILE_DISC
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as [email protected]:1521/orclMEDIAN 中位数CUME_DIST CALCULATES THE CUMULATIVE DISTRIBUTION OF A VALUE IN A GRO原创 2013-08-23 12:47:38 · 2038 阅读 · 0 评论 -
小九九
with l as (select level as lv from dual connect by level <= 9),m as (select a.lv as lv_a, b.lv as lv_b, to_char(b.lv) || ' × ' || to_char(a.lv) || ' = ' || rpad(to_char(a原创 2013-08-23 17:03:34 · 862 阅读 · 0 评论 -
连续值分组
连续值分组 现有如下数据,要求得出连续得冠的起止时间SQL> select * from nba;TEAM Y---------- -----活塞 1990公牛 1991公牛 1992公牛 1993火箭 1994火箭 1995公牛 1996公牛 1原创 2015-07-21 13:46:10 · 1624 阅读 · 0 评论 -
LISTAGG之取索引对应列名称
SQL> select i.TABLE_NAME, 2 i.INDEX_NAME, 3 (select listagg(uc.COLUMN_NAME, ',') within group(order by uc.COLUMN_POSITION) 4 from user_ind_columns uc 5 whe原创 2013-08-12 22:40:51 · 1177 阅读 · 0 评论 -
Oracle 11g r2分析函数新特性(二)分析函数LISTAGG
from http://tech.ddvip.com/2013-09/1378491477202258.html在11gr2中,Oracle分析函数的功能进一步增强。这篇介绍新增的分析函数LISTAGG。11gr2还新增了一个分析函数LISTAGG,这个函数的功能实现字符串的连接在11gr2中,Oracle终于实现了这个分析函数:SQL> select转载 2013-11-06 14:34:14 · 1721 阅读 · 1 评论 -
谜题59_合并时间段
未想到好方法,抛砖引玉而已select start_date, min(end_date) from (select a.start_date, b.end_date from timesheets a, timesheets b where a.end_date <= b.end_date and not exists (sel原创 2013-07-23 20:23:41 · 934 阅读 · 0 评论 -
row_number rank dense_rank
注意工资为3000及2975.00的排序结果SQL> select b.dname, a.ename, a.sal, row_number() over(partition by b.dname order by a.sal desc) as rownumber, rank() over(partition by b.dname orde原创 2013-07-21 11:39:02 · 898 阅读 · 0 评论 -
神奇的IGNORE NULLS
当前行为NULL,想找出该列上一个不为NULL的值,怎么办呢,用IGNORE NULLS吧该方法学自 QQ群内小翁先生(625276799)Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as [email protected]/orcl SQL> SQL> SE转载 2013-07-29 14:02:48 · 2281 阅读 · 0 评论 -
谜题69 入出库之 累加
create table widgetinventory(receipt_nbr integer not null primary key,purchase_date timestamp default current_timestamp,qty_on_hand integer not nullcheck (qty_on_hand >= 0),unit_price decimal(12原创 2013-07-23 20:26:33 · 936 阅读 · 0 评论 -
sql解惑27之相等集合
create table supparts(sno char(2) not null,pno char(2) not null,primary key (sno,pno))insert into suppartsselect '1','1' from dual union allselect '1','2' from dual union allselect '1','3' fr原创 2013-07-25 12:04:52 · 835 阅读 · 0 评论 -
sql解惑2 谜题53 之lag
用刚刚学到的ignore nulls方法原创 2013-07-31 12:32:42 · 1097 阅读 · 0 评论 -
cookbook 14.9 给两次转置的结果集增加列头
cookbook14.9sql有错误,修改后如下select a.it_dept as research, b.it_dept as apps from (select row_number() over(order by deptno, rn) rn, decode(rn, 1, to_char(deptno), ' ' || ename) it_dept原创 2013-08-10 14:39:51 · 658 阅读 · 0 评论 -
累加
SQL> with a as (select rownum as a1 from dual connect by level <=10) 2 select a.a1,sum(a.a1) over (order by a1) as a2 from a 3 / A1 A2---------- ---------- 1原创 2013-07-20 21:25:30 · 881 阅读 · 2 评论 -
Oracle 分析函数(10G)语法详解(转)
from http://www.cnblogs.com/w-pound/archive/2013/05/23/3094865.html(骑牛射雕)Oracle 分析函数(10G)语法详解一、Oracle分析函数简介1、分析函数,最早是从ORACLE8.1.6开始出现的,它的设计目的是为了解决诸如“累计计算”,“找出分组内百分比”,“前-N条查询”,“移动平均数计算”"等转载 2013-08-31 14:17:10 · 1452 阅读 · 0 评论 -
Oracle分析函数参考手册
from http://xsb.itpub.net/post/419/3302822/06/2005 12:22 FPOracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是对于每个组返回多行,而聚合函数对于每个组只返回一行。下面例子中使用的表来自Oracle自带的HR用户下的表,如果没有安装该用户,可以在SYS用户转载 2013-08-31 14:35:46 · 1600 阅读 · 2 评论 -
NTH_VALUE 取排名
SQL> SELECT NTH_VALUE(T.SNAME, 3) 2 FROM FIRST OVER() AS 第三名, NTH_VALUE(T.SNAME, 2) 3 FROM LAST OVER() AS 倒数第二名, T.* 4 FROM STUDENT T 5 ORDER BY AGE;第三名 倒数第二名 SNO SNAME原创 2013-08-30 09:03:59 · 1620 阅读 · 0 评论 -
谜题58_间隔之LAG
连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit ProductionWith the Partitioning, OLAP, Data Mining and Real Application Testing optionsSQL> @set.sql会话已更改。已用时间: 00: 00:原创 2013-07-23 20:22:07 · 837 阅读 · 0 评论 -
用lag补满数据
用lag补满数据现在如下数据SQL> select * from test;DATES KEY----------- ----------2014-01-01 32015-05-01 52015-01-01 4要求把中间缺失的月份补满,结果如下:DATES KEY-------原创 2015-07-17 22:13:37 · 1009 阅读 · 0 评论