하나 이상의 열을 필요로 할 경우
인라인 뷰 구현
스칼라 서브 쿼리 구현
select deptno,
ename,
sal,
sum(sal) over (partition by deptno order by sal) CumDeptTot,
sum(sal) over (partition by deptno) SalByDept,
sum(sal) over (order by deptno, sal) CumTot,
sum(sal) over () TotSal
from emp
order by deptno,sal;
-- 일반적 쿼리
select *
from big_table t1
where last_ddl_time = ( select max(last_ddl_time)
from big_table t2
where t2.owner = t1.owner )
-- 조금 나아진 쿼리
select *
from big_table t1,
( select owner, max(last_ddl_time) max_time
from big_table
group by owner ) t2
where t1.owner = t2.owner
and t1.last_ddl_time = t2.max_time
-- 해석함수를 사용
select owner, last_ddl_time, object_name, object_type
from
( select t1.*,
max(last_ddl_time) over (partition by owner) max_time
from big_table t1
)
where last_ddl_time = max_time;
set linesize 2000
break on deptno skip 1
select deptno,
ename,
sal,
row_number() over (partition by deptno order by sal desc) rn,
rank() over (partition by deptno order by sal desc) rank,
dense_rank() over (partition by deptno order by sal desc) dense_rank
from emp
order by deptno, sal desc;
select deptno,
sal,
dense_rank() over (partition by deptno order by sal desc) dense_rank
from emp;
-- 인라인뷰를 사용한 피벗적용
select deptno,
decode( dense_rank,1, sal ) sal1,
decode( dense_rank,2, sal ) sal2,
decode( dense_rank,3, sal ) sal3
from
( select deptno,
sal,
dense_rank() over (partition by deptno order by sal desc) dense_rank
from emp
)
where dense_rank < 3;
-- NULL 제거
select deptno,
max(decode( dense_rank,1, sal )) sal1,
max(decode( dense_rank,2, sal )) sal2,
max(decode( dense_rank,3, sal )) sal3
from
( select deptno,
sal,
dense_rank() over (partition by deptno order by sal desc) dense_rank
from emp
)
where dense_rank < 3
group by deptno;
FLAG | DATE |
---|---|
FLAG1 | 20091114 |
FLAG1 | 20091116 |
FLAG1 | 20091122 |
FLAG1 | 20091124 |
FLAG1 | 20091128 |
FLAG1 | 20091202 |
FLAG2 | 20091117 |
FLAG2 | 20091119 |
FLAG2 | 20091123 |
FLAG2 | 20091125 |
FLAG2 | 20091127 |
FLAG2 | 20091201 |
FLAG2 | 20091203 |
SELECT FLAG,
DT,
LAG(DT) OVER (PARTITION BY FLAG ORDER BY DT) BF_DT,
LEAD(DT) OVER (PARTITION BY FLAG ORDER BY DT) AF_DT
FROM STUDY_TEST;