Oracle keep()使用

转:http://blog.csdn.net/wanghai__/article/details/5011051

ORACLE中的KEEP()使用方法

2种取值:
DENSE_RANK FIRST 
DENSE_RANK LAST

SQL> select * from test;

ID MC SL
-------------------- -------------------- -------------------
1 111 1
1 222 1
1 333 2
1 555 3
1 666 3
2 111 1
2 222 1
2 333 2
2 555 2

9 rows selected

SQL> 
SQL> select id,mc,sl,
2 min(mc) keep (DENSE_RANK first ORDER BY sl) over(partition by id),
3 max(mc) keep (DENSE_RANK last ORDER BY sl) over(partition by id)
4 from test
5 ;

ID MC SL MIN(MC)KEEP(DENSE_RANKFIRSTORD MAX(MC)KEEP(DENSE_RANKLASTORDE
-------------------- -------------------- ------------------- ------------------------------ ------------------------------
1 111 1 111 666
1 222 1 111 666
1 333 2 111 666
1 555 3 111 666
1 666 3 111 666
2 111 1 111 555
2 222 1 111 555
2 333 2 111 555
2 555 2 111 555

9 rows selected

SQL>

不要混淆keep内(first、last)外(min、max或者其他):
min是可以对应last的
max是可以对应first的
SQL> select id,mc,sl,
2 min(mc) keep (DENSE_RANK first ORDER BY sl) over(partition by id),
3 max(mc) keep (DENSE_RANK first ORDER BY sl) over(partition by id),
4 min(mc) keep (DENSE_RANK last ORDER BY sl) over(partition by id),
5 max(mc) keep (DENSE_RANK last ORDER BY sl) over(partition by id)
6 from test
7 ;

ID MC SL MIN(MC)KEEP(DENSE_RANKFIRSTORD MAX(MC)KEEP(DENSE_RANKFIRSTORD MIN(MC)KEEP(DENSE_RANKLASTORDEMAX(MC)KEEP(DENSE_RANKLASTORDE
-------------------- -------------------- ------------------- ------------------------------ ------------------------------ ------------------------------ ------------------------------
1 111 1 111 222 555 666
1 222 1 111 222 555 666
1 333 2 111 222 555 666
1 555 3 111 222 555 666
1 666 3 111 222 555 666

2 111 1 111 222 333 555
2 222 1 111 222 333 555
2 333 2 111 222 333 555
2 555 2 111 222 333 555

 

 

对于id=1的结果集进行一下解释
min(mc) keep (DENSE_RANK first ORDER BY sl) over(partition by id):id等于1的数量最小的(DENSE_RANK first )为
1 111 1 
1 222 1 
在这个结果中取min(mc) 就是111
max(mc) keep (DENSE_RANK first ORDER BY sl) over(partition by id)
取max(mc) 就是222;
min(mc) keep (DENSE_RANK last ORDER BY sl) over(partition by id):id等于1的数量最大的(DENSE_RANK first )为
1 555 3 
1 666 3

在这个结果中取min(mc) 就是555,取max(mc)就是666

id=2的结果集同理

 

 

 

***********************************************

转:http://blog.csdn.net/java3344520/article/details/5603309

oracle keep(first/last)

 先看一段ORACLE官方文档

http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96520/analysis.htm#25806:

FIRST/LAST Functions

The FIRST/LAST aggregate functions allow you to return the result of an aggregate applied over a set of rows that rank as the first or last with respect to a given order specification. FIRST/LAST lets you order on column A but return an result of an aggregate applied on column B. This is valuable because it avoids the need for a self-join or subquery, thus improving performance. These functions begin with a tiebreaker function, which is a regular aggregate function (MINMAXSUMAVGCOUNTVARIANCESTDDEV) that produces the return value. The tiebreaker function is performed on the set rows (1 or more rows) that rank as first or last respect to the order specification to return a single value.

To specify the ordering used within each group, the FIRST/LAST functions add a new clause starting with the word KEEP.

 

大意是说FIRST/LAST函数按照某个字段排序后取得第一行或者最后一行,FIRST/LAST聚集函数可以按A列排序,B列聚集,避免了自连接和子查询.分组聚合函数(min,max....)位于FIRST/LAST函数之前产生多行结果集,并且按照排序返回FIRST/LAST单个值.

要指定在每个组的顺序,FIRST/LAST函数之前加上以关键字KEEP开始即可

FIRST/LAST Syntax

These functions have the following syntax:

aggregate_function KEEP

( DENSE_RANK LAST ORDER BY

expr [ DESC | ASC ] [NULLS { FIRST | LAST }]

 [, expr [ DESC | ASC ] [NULLS { FIRST | LAST }]]...)

[OVER query_partitioning_clause]

 

Note that the ORDER BY clause can take multiple expressions.请注意在ORDER BY子句可以采取多种表现形式


Returns the row ranked first using DENSE_RANK   

 

 

2种取值:
DENSE_RANK FIRST 
DENSE_RANK LAST 
在keep (DENSE_RANK first ORDER BY sl) 结果集中再取max、min的例子。

 

例子如下:oracle分析函数中,keep and over的区别
公司部门中入厂时间最早的员工的薪水最小的是多少
SQL>SELECT deptno,ename,empno,sal,

MIN(sal) KEEP (dense_rank FIRST ORDER BY hiredate) over (PARTITION BY deptno) "min_sal"
FROM emp;

 

 

DEPTNO ENAME EMPNO HIREDATE SAL min_sal
10 CLARK 7782 1981-06-09 2450.00 2450
10 KING 7839 1981-11-17 5000.00 2450
10 MILLER 7934 1982-01-23 1300.00 2450
20 yang_ping 7389 1980-12-17 2700.00 800
20 SMITH 7369 1980-12-17 800.00 800
20 ADAMS 7876 1987-05-23 1100.00 800
20 FORD  7902 1981-12-03 3000.00 800
20 SCOTT 7788 1987-04-19 4000.00 800
20 JONES 7566 1981-02-22 2975.00 800
30 ALLEN 7499 1981-02-20 1600.00 1600
30 BLAKE 7698 1981-05-01 2850.00 1600
30 MARTIN 7654 1981-09-28 1250.00 1600
30 JAMES 7900 1981-12-03 950.00 1600
30 TURNER 7844 1981-12-03 1500.00 1600
30 WARD 7521 1981-02-22 1250.00 1600

查看结果分析:红色部分,2个入厂日期一样,同时取工资最低得到800

 

 

再看一个:计算部门平均工资,并且入工厂最早的最低的工资

SQL>select deptno,avg(sal) as sal,

min(sal)KEEP (dense_rank FIRST ORDER BY hiredate)  AS min_sal
from emp group by deptno;

 

DEPTNO SAL MIN_SAL
10 3437.5 2450
20 2645.833 800
30 1566.667 1600

你可能感兴趣的:(oracle)