oracle keep(first/last)
2010-05-18 11:02
183 查看
先看一段ORACLE官方文档
To specify the ordering used within each group, the
大意是说FIRST/LAST函数按照某个字段排序后取得第一行或者最后一行,FIRST/LAST聚集函数可以按A列排序,B列聚集,避免了自连接和子查询.分组聚合函数(min,max....)位于FIRST/LAST函数之前产生多行结果集,并且按照排序返回FIRST/LAST单个值.
要指定在每个组的顺序,FIRST/LAST函数之前加上以关键字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
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;
查看结果分析:红色部分,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;
http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96520/analysis.htm#25806:
FIRST/LAST Functions
TheFIRST/LASTaggregate 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/LASTlets 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 (
MIN,
MAX,
SUM,
***G,
COUNT,
VARIANCE,
STDDEV) 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/LASTfunctions 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
BYclause 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 |
再看一个:计算部门平均工资,并且入工厂最早的最低的工资
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分析函数(2) keep(dense_rank first/last) FIRST_VALUE() LAST_VALUE()
- oracle 分析函数 keep(dense_rank first/last)
- oracle分析函数系列之Top/Bottom N、min() keep First/Last、NTile:排名空处理,查询第一名,最后一名
- Oracle分析函数-keep(dense_rank first/last)
- Oracle分析函数——函数RANK,DENSE_RANK,FIRST,LAST
- Oracle分析函数四——函数RANK,DENSE_RANK,FIRST,LAST…
- Oracle应用专题之:分析函数3(Top/Bottom N、First/Last、NTile)
- oracle 和 db2 排序 order by desc/asc nulls last/nulss first 的用法将空值放到最后最前
- Oracle分析函数总结(2) - 排序 - rank,dense_rank,row_number,first,first_value,last,last_value,lag,lead
- Oracle应用专题之:分析函数3(Top/Bottom N、First/Last、NTile)
- Oracle开发之分析函数(Top/Bottom N、First/Last、NTile)
- Oracle应用专题之:分析函数3(Top/Bottom N、First/Last、NTile)
- ORACLE nulls first or nulls last语法学习
- Oracle分析函数四——函数RANK,DENSE_RANK,FIRST,LAST…
- Oracle 分析函数——函数RANK,DENSE_RANK,FIRST,LAST…
- Oracle分析函数四——函数RANK,DENSE_RANK,FIRST,LAST
- 分析函数——keep(dense_rank first/last)
- Oracle分析函数四——函数RANK,DENSE_RANK,FIRST,LAST…
- ORACLE Top/Bottom N、First/Last、NTile
- Oracle应用专题之:分析函数3(Top/Bottom N、First/Last、NTile)