336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
FIRST, LAST (최대, 최소값을 가진 로우 추출)
- 최대, 최소값을 가진 로우를 추출한다.
- 자체로는 함수가 아니며 분석 함수와 같이 사용하는 키워드이다.
[구문 형식]
집계 함수 KEEP ( 함수 FIRST[LAST] ORDER BY expr [ASC | DESC] ) OVER ( PARTITION BY expr .... )
☞ 부서별 최대급여, 최소급여
select dept
, max(salary)
, min(salary)
from (
select 10 dept, 100 id, 39000 salary from dual union all
select 20 dept, 101 id, 30000 salary from dual union all
select 20 dept, 102 id, 9000 salary from dual union all
select 30 dept, 103 id, 17000 salary from dual union all
select 40 dept, 104 id, 60000 salary from dual union all
select 20 dept, 105 id, 9500 salary from dual union all
select 30 dept, 106 id, 17000 salary from dual union all
select 40 dept, 107 id, 37500 salary from dual union all
select 40 dept, 108 id, 25000 salary from dual union all
select 30 dept, 109 id, 5000 salary from dual union all
select 40 dept, 110 id, 17000 salary from dual
)
group by dept;
-----------------------------------
DEPT MAX(SALARY) MIN(SALARY)
-----------------------------------
10 39000 39000
20 30000 9000
30 17000 5000
40 60000 17000
-----------------------------------
☞ 부서별 최대급여와 최소급여를 받는 사원을 동시에 추출해보자.
select dept "부서"
, MAX(id) KEEP ( DENSE_RANK FIRST ORDER BY salary DESC ) "최대급여 사원"
, MAX(salary) "최대급여"
, MIN(id) KEEP ( DENSE_RANK LAST ORDER BY salary DESC ) "최소급여 사원"
, MIN(salary) "최소급여"
from (
select 10 dept, 100 id, 39000 salary from dual union all
select 20 dept, 101 id, 30000 salary from dual union all
select 20 dept, 102 id, 9000 salary from dual union all
select 30 dept, 103 id, 17000 salary from dual union all
select 40 dept, 104 id, 60000 salary from dual union all
select 20 dept, 105 id, 9500 salary from dual union all
select 30 dept, 106 id, 17000 salary from dual union all
select 40 dept, 107 id, 37500 salary from dual union all
select 40 dept, 108 id, 25000 salary from dual union all
select 30 dept, 109 id, 5000 salary from dual union all
select 40 dept, 110 id, 17000 salary from dual
)
group by dept;
------------------------------------------------------------
부서 최대급여 사원 최대급여 최소급여 사원 최소급여
------------------------------------------------------------
10 100 39000 100 39000
20 101 30000 102 9000
30 106 17000 109 5000
40 104 60000 110 17000
------------------------------------------------------------
'DataBase' 카테고리의 다른 글
[Oracle] Show Indexes of Table 테이블에 걸려있는 index 조회 (0) | 2017.04.07 |
---|---|
식별관계( Identifying Relationship ) & 비식별관계( Non Identifying Relationship ) (0) | 2016.04.04 |
[ORACLE] ROW_NUMBER, RANK, DENSE_RANK, 분석용함수 정리 (0) | 2015.11.12 |
ERwin 7.3 설정 (0) | 2015.01.19 |
Partition Table(파티션 테이블) (0) | 2015.01.14 |