2011년 9월 18일 일요일

OVER 함수


WITH TEST AS
(
    select 'A' grade, 10 cnt from dual union all
    select 'A' grade, 20 cnt from dual union all
    select 'A' grade, 15 cnt from dual union all 
    select 'B' grade, 20  from dual union all
    select 'B' grade, 30  from dual union all
    select 'D' grade, 15  from dual union all
    select 'F' grade, 10  from dual
)
SELECT
    grade 등급,
    cnt 학생수, 

    COUNT(1) OVER() CNT,
    sum(cnt) over () 갯수,
    row_number() over(partition by grade order by cnt) 로넘,
    rank() over(partition by grade order by cnt) 랭크,
    round((cnt / sum(cnt) over ())*100,2) 구성비,
    sum(cnt) over (partition by grade  order by grade) 누적학생수,
    sum(cnt) over (partition by grade,cnt  order by grade) 그레카운,
    sum(cnt) over (order by grade) 누적,
    round((sum(cnt) over (order by grade)/sum(cnt) over ())*100,2) 누적구성비
FROM TEST;


/*
  rank() over(partition by grade order by cnt) 랭크,

  표현식 over (partition by 컬럼1 order by 컬럼2)

  이 의미는
  "(컬럼1값이 같은놈들로 묶은것을 컬럼2로 정렬한) 각 파티션별로 표현식을 수행해준다."
*/

댓글 없음:

댓글 쓰기