Oracle SQL 고급 프로 그래 밍 - 분석 함수 (창 함수) 전면 설명
개술
분석 함 수 는 현재 줄 과 관련 된 결 과 를 일정한 방법 으로 집중 적 으로 계산 하 는 것 으로 창 함수 라 고도 부른다.일반 구 조 는 Function (arg 1, arg 2...) over (partition by clause order by clause windowing clause) 입 니 다.
Windowing clause : rows | range between start_expr and end_expr Start_expr is unbounded preceding | current row | n preceding | n following End_expr is unbounded following | current row | n preceding | n following 모든 분석 함수 가 창 문 을 여 는 것 을 지원 하 는 것 은 아 닙 니 다.
테스트 테이블 생 성
SH@ prod> create table sales_fact as
2 select country_name country , country_subregion region , prod_name product , calendar_year year , calendar_week_number week ,
3 sum(amount_sold) sale , sum(amount_sold*
4 (case when mod(rownum , 10 ) = 0 then 1.4
5 when mod(rownum , 5)= 0 then 0.6
6 when mod(rownum , 2)= 0 then 0.9
7 when mod(rownum , 2)=1 then 1.2
8 else 1 end ) ) receipts
9 from sales , times , customers , countries , products
10 where sales.time_id = times.time_id and
11 sales.prod_id = products.prod_id and
12 sales.cust_id = customers.cust_id and
13 customers.country_id = countries.country_id
14 group by country_name , country_subregion , prod_name , calendar_year , calendar_week_number ;
Table created.
취 합 함 수 를 분석 함수 로 사용 하 다.
분석 함수 열 은 하나의 열 수치 일 뿐 모든 줄 은 하나의 값 에 대응 하여 조회 의 다른 측면 에 영향 을 주지 않 습 니 다.
다음 조 회 를 통 해 다음 과 같은 몇 가 지 를 얻 을 수 있 습 니 다. 1. over 파 티 션 조건 의 열 은 selection 목록 에 없 지만 데이터 원본 에 있어 야 합 니 다.2. over 정렬 조건 의 열 은 selection 목록 에 없 을 수 있 지만 데이터 원본 에 있어 야 합 니 다.3. over 정렬 조건 은 파 티 션 에 있 는 데 이 터 를 정렬 하 는 것 으로 selection 문장의 정렬 과 무관 합 니 다.하지만 분석 함수 결과 에 영향 을 미 칠 수 있 습 니 다.4. over 의 창문 을 여 는 조건 의 범 위 는 보통 구역 자체 에 국한 된다.rows between unbounded preceding and current row 는 파 티 션 의 처음부터 현재 줄 까지 를 표시 합 니 다.5. 분석 함수 의 데 이 터 는 결과 집합 (where 조건 을 가 한 후의) 에서 나온다.
아래 조회 에서 분석 열 은 해당 해 부터 이번 주 까지 의 판매 누적 을 나타 낸다.
SH@ prod> select year , week , sale ,
2 sum(sale) over( partition by region , year
3 order by week
4 rows between unbounded preceding and current row ) running_sum_ytd
5 from sales_fact
6 where country in ('Australia') and product='Xtend Memory' and week < 10
7 order by year , week ;
YEAR WEEK SALE RUNNING_SUM_YTD
---------- ---------- ---------- ---------------
1998 1 58.15 58.15
1998 2 29.39 87.54
1998 3 29.49 117.03
1998 4 29.49 146.52
1998 5 29.8 176.32
1998 6 58.78 235.1
1998 9 58.78 293.88
1999 1 53.52 53.52
1999 3 94.6 148.12
1999 4 40.5 188.62
1999 5 80.01 268.63
1999 6 40.5 309.13
1999 8 103.11 412.24
1999 9 53.34 465.58
2000 1 46.7 46.7
2000 3 93.41 140.11
2000 4 46.54 186.65
2000 5 46.7 233.35
2000 7 70.8 304.15
2000 8 46.54 350.69
2001 1 92.26 92.26
2001 2 118.38 210.64
2001 3 47.24 257.88
2001 4 256.7 514.58
2001 5 93.44 608.02
2001 6 22.44 630.46
2001 7 69.96 700.42
YEAR WEEK SALE RUNNING_SUM_YTD
---------- ---------- ---------- ---------------
2001 8 46.06 746.48
2001 9 92.67 839.15
29 rows selected.
결 과 는 위 와 마찬가지 로 정렬 만 다 르 고 분석 열 은 불규칙 해 보인다.
SH@ prod> select year , week , sale ,
2 sum(sale) over( partition by region , year
3 order by week
4 rows between unbounded preceding and current row ) running_sum_ytd
5 from sales_fact
6 where country in ('Australia') and product='Xtend Memory' and week < 10
7 order by year , sale ;
YEAR WEEK SALE RUNNING_SUM_YTD
---------- ---------- ---------- ---------------
1998 2 29.39 87.54
1998 4 29.49 146.52
1998 3 29.49 117.03
1998 5 29.8 176.32
1998 1 58.15 58.15
1998 6 58.78 235.1
1998 9 58.78 293.88
1999 4 40.5 188.62
1999 6 40.5 309.13
1999 9 53.34 465.58
1999 1 53.52 53.52
1999 5 80.01 268.63
1999 3 94.6 148.12
1999 8 103.11 412.24
2000 4 46.54 186.65
2000 8 46.54 350.69
2000 1 46.7 46.7
2000 5 46.7 233.35
2000 7 70.8 304.15
2000 3 93.41 140.11
2001 6 22.44 630.46
2001 8 46.06 746.48
2001 3 47.24 257.88
2001 7 69.96 700.42
2001 1 92.26 92.26
2001 9 92.67 839.15
2001 5 93.44 608.02
YEAR WEEK SALE RUNNING_SUM_YTD
---------- ---------- ---------- ---------------
2001 2 118.38 210.64
2001 4 256.7 514.58
29 rows selected.
파 티 션 의 정렬 선택 이 적절 하지 않 으 면 열 결 과 를 분석 하 는 것 은 아무런 의미 가 없다.파 티 션 창 정렬 에 대한 선택 은 분석 열 결과 와 밀접 한 관 계 를 가진다.
SH@ prod> select year , week , sale ,
2 sum(sale) over( partition by region , year
3 order by sale
4 rows between unbounded preceding and current row ) running_sum_ytd
5 from sales_fact
6 where country in ('Australia') and product='Xtend Memory' and week < 10
7 order by year , week ;
YEAR WEEK SALE RUNNING_SUM_YTD
---------- ---------- ---------- ---------------
1998 1 58.15 176.32
1998 2 29.39 29.39
1998 3 29.49 88.37
1998 4 29.49 58.88
1998 5 29.8 118.17
1998 6 58.78 235.1
1998 9 58.78 293.88
1999 1 53.52 187.86
1999 3 94.6 362.47
1999 4 40.5 40.5
1999 5 80.01 267.87
1999 6 40.5 81
1999 8 103.11 465.58
1999 9 53.34 134.34
2000 1 46.7 186.48
2000 3 93.41 350.69
2000 4 46.54 46.54
2000 5 46.7 139.78
2000 7 70.8 257.28
2000 8 46.54 93.08
2001 1 92.26 277.96
2001 2 118.38 582.45
2001 3 47.24 115.74
2001 4 256.7 839.15
2001 5 93.44 464.07
2001 6 22.44 22.44
2001 7 69.96 185.7
YEAR WEEK SALE RUNNING_SUM_YTD
---------- ---------- ---------- ---------------
2001 8 46.06 68.5
2001 9 92.67 370.63
29 rows selected.
함수 실행 계획 분석
분석 함수 가 있 지만 전체 표 스 캔 을 한 번 만 하면 되 지만 정렬 이 필요 합 니 다.WINDOW SORT 는 분석 함수 의 전형 적 인 특징 이다.
SH@ prod> explain plan for
2 select year , week , sale ,
3 sum(sale) over( partition by region , year
4 order by sale
5 rows between unbounded preceding and current row ) running_sum_ytd
6 from sales_fact
7 where country in ('Australia') and product='Xtend Memory' and week < 10
8 order by year , week ;
Explained.
SH@ prod> select * from table(dbms_xplan.display()) ;
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------
Plan hash value: 173857439
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 18 | 1890 | 311 (1)| 00:00:04 |
| 1 | SORT ORDER BY | | 18 | 1890 | 311 (1)| 00:00:04 |
| 2 | WINDOW SORT | | 18 | 1890 | 311 (1)| 00:00:04 |
|* 3 | TABLE ACCESS FULL| SALES_FACT | 18 | 1890 | 309 (1)| 00:00:04 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("COUNTRY"='Australia' AND "PRODUCT"='Xtend Memory' AND
"WEEK"<10)
Note
-----
- dynamic sampling used for this statement (level=2) 。
20 rows selected.
분석 열 을 추가 하지 않 고 window sort 가 한 걸음 빠 졌 을 뿐 입 니 다.
SH@ prod> explain plan for
2 select year , week , sale
3 from sales_fact
4 where country in ('Australia') and product='Xtend Memory' and week < 10
5 order by year , week ;
Explained.
SH@ prod> select * from table(dbms_xplan.display()) ;
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1978576542
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 18 | 1584 | 310 (1)| 00:00:04 |
| 1 | SORT ORDER BY | | 18 | 1584 | 310 (1)| 00:00:04 |
|* 2 | TABLE ACCESS FULL| SALES_FACT | 18 | 1584 | 309 (1)| 00:00:04 |
---------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("COUNTRY"='Australia' AND "PRODUCT"='Xtend Memory' AND
"WEEK"<10)
Note
-----
- dynamic sampling used for this statement (level=2)
19 rows selected.
어떻게 창 을 전체 파 티 션 으로 가득 채 웁 니까?
SH@ prod> select year , week , sale , max(sale) over(partition by product , country , region , year
2 order by week
3 rows between unbounded preceding and unbounded following )
4 max_sale
5 from sales_fact
6 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
7 order by product , country , year , week ;
YEAR WEEK SALE MAX_SALE
---------- ---------- ---------- ----------
1998 1 58.15 58.78
1998 2 29.39 58.78
1998 3 29.49 58.78
1998 4 29.49 58.78
1998 5 29.8 58.78
1998 6 58.78 58.78
1998 9 58.78 58.78
1999 1 53.52 103.11
1999 3 94.6 103.11
1999 4 40.5 103.11
1999 5 80.01 103.11
1999 6 40.5 103.11
1999 8 103.11 103.11
1999 9 53.34 103.11
2000 1 46.7 93.41
2000 3 93.41 93.41
2000 4 46.54 93.41
2000 5 46.7 93.41
2000 7 70.8 93.41
2000 8 46.54 93.41
2001 1 92.26 256.7
2001 2 118.38 256.7
2001 3 47.24 256.7
2001 4 256.7 256.7
2001 5 93.44 256.7
2001 6 22.44 256.7
2001 7 69.96 256.7
YEAR WEEK SALE MAX_SALE
---------- ---------- ---------- ----------
2001 8 46.06 256.7
2001 9 92.67 256.7
29 rows selected.
두 경계 가 모두 미 끄 러 지 는 창
아래 문장의 창 은 앞으로 2 주, 앞으로 2 주, 현재 주 를 더 하면 모두 5 주 입 니 다.(경계 에 도달 하면 창 이 자동 으로 축 소 됩 니 다)
SH@ prod> select year , week , sale , max(sale) over(partition by product , country , region , year
2 order by week
3 rows between 2 preceding and 2 following )
4 max_sale
5 from sales_fact
6 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
7 order by product , country , year , week ;
YEAR WEEK SALE MAX_SALE
---------- ---------- ---------- ----------
1998 1 58.15 58.15
1998 2 29.39 58.15
1998 3 29.49 58.15
1998 4 29.49 58.78
1998 5 29.8 58.78
1998 6 58.78 58.78
1998 9 58.78 58.78
1999 1 53.52 94.6
1999 3 94.6 94.6
1999 4 40.5 94.6
1999 5 80.01 103.11
1999 6 40.5 103.11
1999 8 103.11 103.11
1999 9 53.34 103.11
2000 1 46.7 93.41
2000 3 93.41 93.41
2000 4 46.54 93.41
2000 5 46.7 93.41
2000 7 70.8 70.8
2000 8 46.54 70.8 70.8 。
2001 1 92.26 118.38
2001 2 118.38 256.7
2001 3 47.24 256.7
2001 4 256.7 256.7
2001 5 93.44 256.7
2001 6 22.44 256.7
2001 7 69.96 93.44
YEAR WEEK SALE MAX_SALE
---------- ---------- ---------- ----------
2001 8 46.06 92.67
2001 9 92.67 92.67
29 rows selected.
기본 창 이 무엇 입 니까?
한눈 에 알다.
SH@ prod> select year , week , sale , max(sale) over(partition by product , country , region , year
2 order by week )
3 max_sale
4 from sales_fact
5 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
6 order by product , country , year , week ;
YEAR WEEK SALE MAX_SALE
---------- ---------- ---------- ----------
1998 1 58.15 58.15
1998 2 29.39 58.15
1998 3 29.49 58.15
1998 4 29.49 58.15
1998 5 29.8 58.15
1998 6 58.78 58.78
1998 9 58.78 58.78
1999 1 53.52 53.52
1999 3 94.6 94.6
1999 4 40.5 94.6
1999 5 80.01 94.6
1999 6 40.5 94.6
1999 8 103.11 103.11
1999 9 53.34 103.11
2000 1 46.7 46.7
2000 3 93.41 93.41
2000 4 46.54 93.41
2000 5 46.7 93.41
2000 7 70.8 93.41
2000 8 46.54 93.41
2001 1 92.26 92.26
2001 2 118.38 118.38
2001 3 47.24 118.38
2001 4 256.7 256.7
2001 5 93.44 256.7
2001 6 22.44 256.7
2001 7 69.96 256.7
YEAR WEEK SALE MAX_SALE
---------- ---------- ---------- ----------
2001 8 46.06 256.7
2001 9 92.67 256.7
29 rows selected.
Lead 와 Lag (창 을 여 는 함수 가 지원 되 지 않 음)
창문 을 열 때 이런 실 수 를 한다.
rows between 2 preceding and 2 following )
*
ERROR at line 3:
ORA-00907: missing right parenthesis
LEAD 는 이전 이 아니 라 다음 을 구 하 는 것 이다.파 티 션 의 아래 경계 에서 LEAD 에서 빈 값 을 되 돌려 줍 니 다.
SH@ prod> select year , week , sale , lead(sale) over(partition by product , country , region , year
2 order by week )
3 former_sale
4 from sales_fact
5 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
6 order by product , country , year , week ;
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
1998 1 58.15 29.39
1998 2 29.39 29.49
1998 3 29.49 29.49
1998 4 29.49 29.8
1998 5 29.8 58.78
1998 6 58.78 58.78
1998 9 58.78
1999 1 53.52 94.6
1999 3 94.6 40.5
1999 4 40.5 80.01
1999 5 80.01 40.5
1999 6 40.5 103.11
1999 8 103.11 53.34
1999 9 53.34
2000 1 46.7 93.41
2000 3 93.41 46.54
2000 4 46.54 46.7
2000 5 46.7 70.8
2000 7 70.8 46.54
2000 8 46.54
2001 1 92.26 118.38
2001 2 118.38 47.24
2001 3 47.24 256.7
2001 4 256.7 93.44
2001 5 93.44 22.44
2001 6 22.44 69.96
2001 7 69.96 46.06
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
2001 8 46.06 92.67
2001 9 92.67
29 rows selected.
LAG 는 이전, 즉 이전 을 구 했다.파 티 션 의 상단 경계 에서 빈 값 을 되 돌려 줍 니 다.
SH@ prod> select year , week , sale , lag(sale) over(partition by product , country , region , year
2 order by week )
3 former_sale
4 from sales_fact
5 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
6 order by product , country , year , week ;
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
1998 1 58.15
1998 2 29.39 58.15
1998 3 29.49 29.39
1998 4 29.49 29.49
1998 5 29.8 29.49
1998 6 58.78 29.8
1998 9 58.78 58.78
1999 1 53.52
1999 3 94.6 53.52
1999 4 40.5 94.6
1999 5 80.01 40.5
1999 6 40.5 80.01
1999 8 103.11 40.5
1999 9 53.34 103.11
2000 1 46.7
2000 3 93.41 46.7
2000 4 46.54 93.41
2000 5 46.7 46.54
2000 7 70.8 46.7
2000 8 46.54 70.8
2001 1 92.26
2001 2 118.38 92.26
2001 3 47.24 118.38
2001 4 256.7 47.24
2001 5 93.44 256.7
2001 6 22.44 93.44
2001 7 69.96 22.44
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
2001 8 46.06 69.96
2001 9 92.67 46.06
29 rows selected.
복잡 한 Lead 와 Lag.
Lead 와 lag 함수 의 첫 번 째 매개 변 수 는 되 돌아 오 는 열 이 고 두 번 째 매개 변 수 는 간격 줄 수 (비 마이너스) 이 며 세 번 째 매개 변 수 는 존재 하지 않 을 때의 기본 값 (현재 줄 의 값 으로 지정 할 수 있 음) 입 니 다.
SH@ prod> select year , week , sale , lag(sale , 2 , 0 ) over(partition by product , country , region , year
2 order by week )
3 former_sale
4 from sales_fact
5 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
6 order by product , country , year , week ;
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
1998 1 58.15 0
1998 2 29.39 0
1998 3 29.49 58.15
1998 4 29.49 29.39
1998 5 29.8 29.49
1998 6 58.78 29.49
1998 9 58.78 29.8
1999 1 53.52 0
1999 3 94.6 0
1999 4 40.5 53.52
1999 5 80.01 94.6
1999 6 40.5 40.5
1999 8 103.11 80.01
1999 9 53.34 40.5
2000 1 46.7 0
2000 3 93.41 0
2000 4 46.54 46.7
2000 5 46.7 93.41
2000 7 70.8 46.54
2000 8 46.54 46.7
2001 1 92.26 0
2001 2 118.38 0
2001 3 47.24 92.26
2001 4 256.7 118.38
2001 5 93.44 47.24
2001 6 22.44 256.7
2001 7 69.96 93.44
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
2001 8 46.06 22.44
2001 9 92.67 69.96
29 rows selected.
기본 값 을 현재 줄 의 값 으로 지정 합 니 다.
SH@ prod> select year , week , sale , lag(sale , 2 , sale ) over(partition by product , country , region , year
2 order by week )
3 former_sale
4 from sales_fact
5 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
6 order by product , country , year , week ;
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
1998 1 58.15 58.15
1998 2 29.39 29.39
1998 3 29.49 58.15
1998 4 29.49 29.39
1998 5 29.8 29.49
1998 6 58.78 29.49
1998 9 58.78 29.8
1999 1 53.52 53.52
1999 3 94.6 94.6
1999 4 40.5 53.52
1999 5 80.01 94.6
1999 6 40.5 40.5
1999 8 103.11 80.01
1999 9 53.34 40.5
2000 1 46.7 46.7
2000 3 93.41 93.41
2000 4 46.54 46.7
2000 5 46.7 93.41
2000 7 70.8 46.54
2000 8 46.54 46.7
2001 1 92.26 92.26
2001 2 118.38 118.38
2001 3 47.24 92.26
2001 4 256.7 118.38
2001 5 93.44 47.24
2001 6 22.44 256.7
2001 7 69.96 93.44
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
2001 8 46.06 22.44
2001 9 92.67 69.96
29 rows selected.
LEAD 와 LAG 의 데이터 부족 에 대한 문제
LAG (sale, 10) 는 10 줄 떨 어 진 데 이 터 를 표시 하지만 내 가 방문 하고 싶 은 10 주 전의 데 이 터 를 표시 합 니 다.중간 데이터 에 결함 이 있 으 면 심각 한 문제 가 발생 할 수 있다.
FIRST_VALUE 와 LASTVALUE
이 두 함 수 는 모두 orderby 조건 과 결합 하여 최대 치 와 최소 치 를 얻 을 수 있다.First_value 는 창의 첫 번 째 값 을 되 돌려 줍 니 다.Ignore nulls 는 빈 값 을 무시 하고 첫 번 째 가 빈 값 이 라면 두 번 째 로 되 돌려 줍 니 다.
SH@ prod> select year , week , sale , first_value(sale ignore nulls) over(partition by product , country , region , year
2 order by week
3 rows between unbounded preceding and unbounded following )
4 former_sale
5 from sales_fact
6 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
7 order by product , country , year , week ;
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
1998 1 58.15 58.15
1998 2 29.39 58.15
1998 3 29.49 58.15
1998 4 29.49 58.15
1998 5 29.8 58.15
1998 6 58.78 58.15
1998 9 58.78 58.15
1999 1 53.52 53.52
1999 3 94.6 53.52
1999 4 40.5 53.52
1999 5 80.01 53.52
1999 6 40.5 53.52
1999 8 103.11 53.52
1999 9 53.34 53.52
2000 1 46.7 46.7
2000 3 93.41 46.7
2000 4 46.54 46.7
2000 5 46.7 46.7
2000 7 70.8 46.7
2000 8 46.54 46.7
2001 1 92.26 92.26
2001 2 118.38 92.26
2001 3 47.24 92.26
2001 4 256.7 92.26
2001 5 93.44 92.26
2001 6 22.44 92.26
2001 7 69.96 92.26
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
2001 8 46.06 92.26
2001 9 92.67 92.26
29 rows selected.
Last_value 는 창의 마지막 값 을 되 돌려 줍 니 다.Respect nulls 는 빈 값 을 식별 하고 마지막 이 빈 값 이 라면 되 돌려 줍 니 다.
SH@ prod> select year , week , sale , last_value(sale respect nulls) over(partition by product , country , region , year
2 order by week
3 rows between unbounded preceding and unbounded following )
4 former_sale
5 from sales_fact
6 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
7 order by product , country , year , week ;
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
1998 1 58.15 58.78
1998 2 29.39 58.78
1998 3 29.49 58.78
1998 4 29.49 58.78
1998 5 29.8 58.78
1998 6 58.78 58.78
1998 9 58.78 58.78
1999 1 53.52 53.34
1999 3 94.6 53.34
1999 4 40.5 53.34
1999 5 80.01 53.34
1999 6 40.5 53.34
1999 8 103.11 53.34
1999 9 53.34 53.34
2000 1 46.7 46.54
2000 3 93.41 46.54
2000 4 46.54 46.54
2000 5 46.7 46.54
2000 7 70.8 46.54
2000 8 46.54 46.54
2001 1 92.26 92.67
2001 2 118.38 92.67
2001 3 47.24 92.67
2001 4 256.7 92.67
2001 5 93.44 92.67
2001 6 22.44 92.67
2001 7 69.96 92.67
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
2001 8 46.06 92.67
2001 9 92.67 92.67
29 rows selected.
NTH_VALUE 접근 구분 임 의 지정 줄
FIRST_VALUE 는 NTH 에 해당 합 니 다.VALUE (sale, 1) 또는 NTHVALUE(sale , 1 )from first respect nulls。 정렬 과 배합 하여 몇 번 째 크 고 몇 번 째 작은 것 을 구 할 수 있다.
SH@ prod> select year , week , sale , nth_value(sale , 1 ) from last ignore nulls over(partition by product , country , region , year
2 order by week
3 rows between unbounded preceding and unbounded following )
4 former_sale
5 from sales_fact
6 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
7 order by product , country , year , week ;
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
1998 1 58.15 58.78
1998 2 29.39 58.78
1998 3 29.49 58.78
1998 4 29.49 58.78
1998 5 29.8 58.78
1998 6 58.78 58.78
1998 9 58.78 58.78
1999 1 53.52 53.34
1999 3 94.6 53.34
1999 4 40.5 53.34
1999 5 80.01 53.34
1999 6 40.5 53.34
1999 8 103.11 53.34
1999 9 53.34 53.34
2000 1 46.7 46.54
2000 3 93.41 46.54
2000 4 46.54 46.54
2000 5 46.7 46.54
2000 7 70.8 46.54
2000 8 46.54 46.54
2001 1 92.26 92.67
2001 2 118.38 92.67
2001 3 47.24 92.67
2001 4 256.7 92.67
2001 5 93.44 92.67
2001 6 22.44 92.67
2001 7 69.96 92.67
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
2001 8 46.06 92.67
2001 9 92.67 92.67
29 rows selected.
RANK 함수 (창 을 열 수 없고 전체 파 티 션 에 작용 합 니 다)
랭 킹 은 정렬 조건 이 있어 야 합 니 다. 랭 킹 은 order by 조건 중의 열 에 따라 순 위 를 정 합 니 다.RANK 함수 순위 에 서 는 공동 순위 가 나 오 면 순위 가 연속 되 지 않 는 다.예 를 들 어 12 (2) 45, 6, 7, 89.2 등 이 두 개 면 3 등 은 존재 하지 않 는 다.빈 값 을 주의 하 십시오. 정렬 자구 에서 NULLS LAST 를 사용 하여 빈 값 을 맨 뒤에 놓 을 수 있 습 니 다.
SH@ prod> select year , week , sale , rank() over(partition by product , country , region , year
2 order by sale )
3 former_sale
4 from sales_fact
5 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
6 order by product , country , year , week ;
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
1998 1 58.15 5 3
1998 2 29.39 1
1998 3 29.49 2
1998 4 29.49 2
1998 5 29.8 4
1998 6 58.78 6
1998 9 58.78 6
1999 1 53.52 4
1999 3 94.6 6
1999 4 40.5 1
1999 5 80.01 5
1999 6 40.5 1
1999 8 103.11 7
1999 9 53.34 3
2000 1 46.7 3
2000 3 93.41 6
2000 4 46.54 1
2000 5 46.7 3
2000 7 70.8 5
2000 8 46.54 1
2001 1 92.26 5
2001 2 118.38 8
2001 3 47.24 3
2001 4 256.7 9
2001 5 93.44 7
2001 6 22.44 1
2001 7 69.96 4
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
2001 8 46.06 2
2001 9 92.67 6
29 rows selected.
DENSE_RANK.
SH@ prod> select year , week , sale , dense_rank() over(partition by product , country , region , year
2 order by sale )
3 former_sale
4 from sales_fact
5 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
6 order by product , country , year , week ;
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
1998 1 58.15 4
1998 2 29.39 1
1998 3 29.49 2
1998 4 29.49 2
1998 5 29.8 3
1998 6 58.78 5
1998 9 58.78 5
1999 1 53.52 3
1999 3 94.6 5
1999 4 40.5 1
1999 5 80.01 4
1999 6 40.5 1
1999 8 103.11 6
1999 9 53.34 2
2000 1 46.7 2
2000 3 93.41 4
2000 4 46.54 1
2000 5 46.7 2
2000 7 70.8 3
2000 8 46.54 1
2001 1 92.26 5
2001 2 118.38 8
2001 3 47.24 3
2001 4 256.7 9
2001 5 93.44 7
2001 6 22.44 1
2001 7 69.96 4
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
2001 8 46.06 2
2001 9 92.67 6
29 rows selected.
ROW_NUMBER (창 열기, 불확실 성 함수 지원 하지 않 음)
파 티 션 의 모든 줄 에 증가 하 는 번 호 를 지정 합 니 다. 정렬 된 열의 값 이 같 으 면 누가 먼저 누 구 를 먼저 하고 무 작위 로 합 니까?
SH@ prod> select year , week , sale , row_number() over(partition by product , country , region , year
2 order by sale )
3 former_sale
4 from sales_fact
5 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
6 order by product , country , year , sale ;
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
1998 2 29.39 1
1998 4 29.49 2
1998 3 29.49 3
1998 5 29.8 4
1998 1 58.15 5
1998 6 58.78 6
1998 9 58.78 7
1999 4 40.5 1
1999 6 40.5 2
1999 9 53.34 3
1999 1 53.52 4
1999 5 80.01 5
1999 3 94.6 6
1999 8 103.11 7
2000 4 46.54 1
2000 8 46.54 2
2000 5 46.7 3
2000 1 46.7 4
2000 7 70.8 5
2000 3 93.41 6
2001 6 22.44 1
2001 8 46.06 2
2001 3 47.24 3
2001 7 69.96 4
2001 1 92.26 5
2001 9 92.67 6
2001 5 93.44 7
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
2001 2 118.38 8
2001 4 256.7 9
29 rows selected.
Ratio_to_report (현재 줄 의 값 과 파 티 션 의 합계 값)
이 함 수 는 정렬 과 창 을 여 는 것 을 지원 하지 않 습 니 다.각 주의 판 매 량 이 매년 중의 비율 과 전체 제품 판 매 량 에서 의 비율 을 구하 다.
SH@ prod> select year , week , sale ,
2 trunc(100* ratio_to_report(sale) over(partition by year ) , 2) sales_yr ,
3 trunc(100* ratio_to_report(sale) over() , 2 ) sales_prod
4 from sales_fact
5 where country in ('Australia') and product = 'Xtend Memory' and week < 10
6 order by year , week ;
YEAR WEEK SALE SALES_YR SALES_PROD
---------- ---------- ---------- ---------- ----------
1998 1 58.15 19.78 2.98
1998 2 29.39 10 1.5
1998 3 29.49 10.03 1.51
1998 4 29.49 10.03 1.51
1998 5 29.8 10.14 1.52
1998 6 58.78 20 3.01
1998 9 58.78 20 3.01
1999 1 53.52 11.49 2.74
1999 3 94.6 20.31 4.85
1999 4 40.5 8.69 2.07
1999 5 80.01 17.18 4.1
1999 6 40.5 8.69 2.07
1999 8 103.11 22.14 5.28
1999 9 53.34 11.45 2.73
2000 1 46.7 13.31 2.39
2000 3 93.41 26.63 4.79
2000 4 46.54 13.27 2.38
2000 5 46.7 13.31 2.39
2000 7 70.8 20.18 3.63
2000 8 46.54 13.27 2.38
2001 1 92.26 10.99 4.73
2001 2 118.38 14.1 6.07
2001 3 47.24 5.62 2.42
2001 4 256.7 30.59 13.16
2001 5 93.44 11.13 4.79
2001 6 22.44 2.67 1.15
2001 7 69.96 8.33 3.58
YEAR WEEK SALE SALES_YR SALES_PROD
---------- ---------- ---------- ---------- ----------
2001 8 46.06 5.48 2.36
2001 9 92.67 11.04 4.75
29 rows selected.
Percent_rank (상위 몇%)
현재 줄 의 순위 의 상대 적 인 100% 위 치 를 구 하 는 데 사용 합 니 다.예 를 들 어 당신 이 다른 사람 에 게 자신 이 10 등 이 라 고 말 하면 다른 사람들 은 아무것도 아니 라 고 생각 할 수도 있 습 니 다. 만약 에 100000 중의 10 등 이 라면 앞의 1 / 10000 이 라면 매우 대단 합 니 다.이 함수 와 RANK 의 유도 공식 은 PERCENT 이다.RANK = (RANK - 1) / (N - 1), N 은 본점 수 를 나타 낸다.RANK – 1 대표 순 위 는 자신의 인원수 보다 크다.N - 1 은 자신 을 제외 한 총 인원 을 대표 한다.전체적으로 자신 을 제외 한 다른 사람 이 자신 보다 순위 가 높 은 사람 이 차지 하 는 비율 을 의미한다.
SH@ prod> select year , week , sale , rank() over(partition by product , country , region , year
2 order by sale )
3 former_sale
4 from sales_fact
5 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
6 order by product , country , year , sale ;
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
1998 2 29.39 1
1998 4 29.49 2
1998 3 29.49 2
1998 5 29.8 4
1998 1 58.15 5
1998 6 58.78 6
1998 9 58.78 6
1999 4 40.5 1
1999 6 40.5 1
1999 9 53.34 3
1999 1 53.52 4
1999 5 80.01 5
1999 3 94.6 6
1999 8 103.11 7
2000 4 46.54 1
2000 8 46.54 1
2000 5 46.7 3
2000 1 46.7 3
2000 7 70.8 5
2000 3 93.41 6
2001 6 22.44 1
2001 8 46.06 2
2001 3 47.24 3
2001 7 69.96 4
2001 1 92.26 5
2001 9 92.67 6
2001 5 93.44 7
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
2001 2 118.38 8
2001 4 256.7 9
29 rows selected.
SH@ prod> select year , week , sale , 100*percent_rank() over(partition by product , country , region , year
2 order by sale )
3 former_sale
4 from sales_fact
5 where country in ( 'Australia') and product = 'Xtend Memory' and week < 10
6 order by product , country , year , sale ;
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
1998 2 29.39 0
1998 4 29.49 16.6666667
1998 3 29.49 16.6666667
1998 5 29.8 50
1998 1 58.15 66.6666667
1998 6 58.78 83.3333333
1998 9 58.78 83.3333333
1999 4 40.5 0
1999 6 40.5 0
1999 9 53.34 33.3333333
1999 1 53.52 50
1999 5 80.01 66.6666667
1999 3 94.6 83.3333333
1999 8 103.11 100
2000 4 46.54 0
2000 8 46.54 0
2000 5 46.7 40
2000 1 46.7 40
2000 7 70.8 80
2000 3 93.41 100
2001 6 22.44 0
2001 8 46.06 12.5
2001 3 47.24 25
2001 7 69.96 37.5
2001 1 92.26 50
2001 9 92.67 62.5
2001 5 93.44 75
YEAR WEEK SALE FORMER_SALE
---------- ---------- ---------- -----------
2001 2 118.38 87.5
2001 4 256.7 100
29 rows selected.
Percentile_cont (대체 적 으로 백분율 에 필요 한 수 치 를 구 합 니 다)
현재 이 값 을 파 티 션 에 삽입 하고 순 위 는 N% (percent rank 은 N%) 로 이 값 을 구 하 는 것 이 라 고 할 수 있 습 니 다.줄 이 있 는 percentrank 는 N 과 같 습 니 다. 그러면 이것 입 니까?일치 하 는 것 이 없 으 면 확률 이 가장 높다.
SH@ prod> select year , week , sale ,
2 percentile_cont(0.5) within group(order by sale desc )over(partition by year) pc ,
3 percent_rank() over( partition by year order by sale desc ) pr
4 from sales_fact
5 where country in ('Australia') and product = 'Xtend Memory' and week < 11 ;
YEAR WEEK SALE PC PR
---------- ---------- ---------- ---------- ----------
1998 10 117.76 43.975 0
1998 9 58.78 43.975 .142857143
1998 6 58.78 43.975 .142857143
1998 1 58.15 43.975 .428571429
1998 5 29.8 43.975 .571428571
1998 3 29.49 43.975 .714285714
1998 4 29.49 43.975 .714285714
1998 2 29.39 43.975 1
1999 8 103.11 62.76 0
1999 3 94.6 62.76 .142857143
1999 5 80.01 62.76 .285714286
1999 10 72 62.76 .428571429
1999 1 53.52 62.76 .571428571
1999 9 53.34 62.76 .714285714
1999 6 40.5 62.76 .857142857
1999 4 40.5 62.76 .857142857
2000 3 93.41 46.7 0
2000 7 70.8 46.7 .2
2000 5 46.7 46.7 .4
2000 1 46.7 46.7 .4
2000 4 46.54 46.7 .8
2000 8 46.54 46.7 .8
2001 4 256.7 81.11 0
2001 2 118.38 81.11 .111111111
2001 5 93.44 81.11 .222222222
2001 9 92.67 81.11 .333333333
2001 1 92.26 81.11 .444444444
YEAR WEEK SALE PC PR
---------- ---------- ---------- ---------- ----------
2001 7 69.96 81.11 .555555556
2001 10 69.05 81.11 .666666667
2001 3 47.24 81.11 .777777778
2001 8 46.06 81.11 .888888889
2001 6 22.44 81.11 1
32 rows selected.
Percentile_disc (기능 은 Percentile cont 와 대체적으로 같 음)
차이 점 은 이 함수 가 얻 은 값 이 반드시 이 구역 의 줄 에 있다 는 것 이다.일치 하 는 것 이 없 으 면 Percentiledisc 는 정렬 에 따라 하 나 를 가 져 옵 니 다.
SH@ prod> select year , week , sale ,
2 percentile_disc(0.5) within group(order by sale desc )over(partition by year) pc ,
3 percent_rank() over( partition by year order by sale desc ) pr
4 from sales_fact
5 where country in ('Australia') and product = 'Xtend Memory' and week < 11 ;
YEAR WEEK SALE PC PR
---------- ---------- ---------- ---------- ----------
1998 10 117.76 58.15 0
1998 9 58.78 58.15 .142857143
1998 6 58.78 58.15 .142857143
1998 1 58.15 58.15 .428571429
1998 5 29.8 58.15 .571428571
1998 3 29.49 58.15 .714285714
1998 4 29.49 58.15 .714285714
1998 2 29.39 58.15 1
1999 8 103.11 72 0
1999 3 94.6 72 .142857143
1999 5 80.01 72 .285714286
1999 10 72 72 .428571429
1999 1 53.52 72 .571428571
1999 9 53.34 72 .714285714
1999 6 40.5 72 .857142857
1999 4 40.5 72 .857142857
2000 3 93.41 46.7 0
2000 7 70.8 46.7 .2
2000 5 46.7 46.7 .4
2000 1 46.7 46.7 .4
2000 4 46.54 46.7 .8
2000 8 46.54 46.7 .8
2001 4 256.7 92.26 0
2001 2 118.38 92.26 .111111111
2001 5 93.44 92.26 .222222222
2001 9 92.67 92.26 .333333333
2001 1 92.26 92.26 .444444444
YEAR WEEK SALE PC PR
---------- ---------- ---------- ---------- ----------
2001 7 69.96 92.26 .555555556
2001 10 69.05 92.26 .666666667
2001 3 47.24 92.26 .777777778
2001 8 46.06 92.26 .888888889
2001 6 22.44 92.26 1
32 rows selected.
SH@ prod> select year , week , sale ,
2 percentile_cont(0.5) within group(order by sale desc )over(partition by year) pc ,
3 percent_rank() over( partition by year order by sale desc ) pr
4 from sales_fact
5 where country in ('Australia') and product = 'Xtend Memory' and week < 11 ;
YEAR WEEK SALE PC PR
---------- ---------- ---------- ---------- ----------
1998 10 117.76 43.975 0
1998 9 58.78 43.975 .142857143
1998 6 58.78 43.975 .142857143
1998 1 58.15 43.975 .428571429
1998 5 29.8 43.975 .571428571
1998 3 29.49 43.975 .714285714
1998 4 29.49 43.975 .714285714
1998 2 29.39 43.975 1
1999 8 103.11 62.76 0
1999 3 94.6 62.76 .142857143
1999 5 80.01 62.76 .285714286
1999 10 72 62.76 .428571429
1999 1 53.52 62.76 .571428571
1999 9 53.34 62.76 .714285714
1999 6 40.5 62.76 .857142857
1999 4 40.5 62.76 .857142857
2000 3 93.41 46.7 0
2000 7 70.8 46.7 .2
2000 5 46.7 46.7 .4
2000 1 46.7 46.7 .4
2000 4 46.54 46.7 .8
2000 8 46.54 46.7 .8
2001 4 256.7 81.11 0
2001 2 118.38 81.11 .111111111
2001 5 93.44 81.11 .222222222
2001 9 92.67 81.11 .333333333
2001 1 92.26 81.11 .444444444
YEAR WEEK SALE PC PR
---------- ---------- ---------- ---------- ----------
2001 7 69.96 81.11 .555555556
2001 10 69.05 81.11 .666666667
2001 3 47.24 81.11 .777777778
2001 8 46.06 81.11 .888888889
2001 6 22.44 81.11 1
32 rows selected.
NTILE (직사 도 를 만 드 는 형식 으로 창 을 여 는 것 은 지원 되 지 않 습 니 다)
정렬 된 데 이 터 를 지정 한 데이터 통 에 골 고루 분배 하고 통 번 호 를 되 돌려 줍 니 다. 등분 할 수 없 으 면 각 통 의 줄 수 는 최대 한 줄 차이 가 납 니 다.이후 처리 에 서 는 첫 통 이나 꼬리 를 제거 해 이상 치 를 제거 할 수 있다.주의: 값 에 따라 분배 되 는 것 이 아 닙 니 다.
SH@ prod> select year , week , sale ,
2 ntile(10) over(order by sale ) group#
3 from sales_fact
4 where country in ('Australia') and product = 'Xtend Memory' and year = 1998 order by year , sale;
YEAR WEEK SALE GROUP#
---------- ---------- ---------- ----------
1998 50 28.76 1
1998 2 29.39 1
1998 4 29.49 1
1998 3 29.49 1
1998 5 29.8 2
1998 43 57.52 2
1998 35 57.52 2
1998 40 57.52 2
1998 46 57.52 3
1998 27 57.52 3
1998 45 57.52 3
1998 44 57.52 3
1998 47 57.72 4
1998 29 57.72 4
1998 28 57.72 4
1998 1 58.15 4
1998 41 58.32 5
1998 51 58.32 5
1998 14 58.78 5
1998 9 58.78 5
1998 15 58.78 6
1998 17 58.78 6
1998 6 58.78 6
1998 19 58.98 6
1998 21 59.6 7
1998 12 59.6 7
1998 52 86.38 7
YEAR WEEK SALE GROUP#
---------- ---------- ---------- ----------
1998 34 115.44 8
1998 39 115.84 8
1998 42 115.84 8
1998 38 115.84 9
1998 23 117.56 9
1998 18 117.56 9
1998 26 117.56 10
1998 10 117.76 10
1998 48 172.56 10
36 rows selected.
Stddev 계산 표준 차 (방 차 의 제곱 근, 창 열기 지원)
SH@ prod> select year , week , sale ,
2 stddev(sale) over(
3 partition by product , country , region , year
4 order by sale desc
5 rows between 2 preceding and 2 following ) stddv
6 from sales_fact
7 where country in ('Australia') and product = 'Xtend Memory' and week < 10
8 order by year , week ;
YEAR WEEK SALE STDDV
---------- ---------- ---------- ----------
1998 1 58.15 15.8453416
1998 2 29.39 .057735027
1998 3 29.49 .178021534
1998 4 29.49 12.7945918
1998 5 29.8 15.815738
1998 6 58.78 .36373067
1998 9 58.78 14.3880654
1999 1 53.52 22.178931
1999 3 94.6 21.7319902
1999 4 40.5 7.46550065
1999 5 80.01 22.9761992
1999 6 40.5 7.41317746
1999 8 103.11 11.6825953
1999 9 53.34 16.1305511
2000 1 46.7 21.0022332
2000 3 93.41 23.3589605
2000 4 46.54 .092376043
2000 5 46.7 10.8139207
2000 7 70.8 22.4285538
2000 8 46.54 .092376043
2001 1 92.26 20.3811452
2001 2 118.38 78.5152276
2001 3 47.24 26.5077898
2001 4 256.7 87.947194
2001 5 93.44 71.309193
2001 6 22.44 13.9900965
2001 7 69.96 22.9124643
YEAR WEEK SALE STDDV
---------- ---------- ---------- ----------
2001 8 46.06 19.407678
2001 9 92.67 17.1409691
29 rows selected.
Listagg (파 티 션 의 열 을 순서대로 연결 하고 창 을 여 는 것 은 지원 되 지 않 습 니 다)
SH@ prod> col stddv for a60
SH@ prod> select year , week , sale ,
2 listagg(sale , ' , ')within group(order by sale desc) over(
3 partition by product , country , region , year ) stddv
4 from sales_fact
5 where country in ('Australia') and product = 'Xtend Memory' and week < 5
6 order by year , week ;
YEAR WEEK SALE STDDV
---------- ---------- ---------- ------------------------------------------------------------
1998 1 58.15 58.15 , 29.49 , 29.49 , 29.39
1998 2 29.39 58.15 , 29.49 , 29.49 , 29.39
1998 3 29.49 58.15 , 29.49 , 29.49 , 29.39
1998 4 29.49 58.15 , 29.49 , 29.49 , 29.39
1999 1 53.52 94.6 , 53.52 , 40.5
1999 3 94.6 94.6 , 53.52 , 40.5
1999 4 40.5 94.6 , 53.52 , 40.5
2000 1 46.7 93.41 , 46.7 , 46.54
2000 3 93.41 93.41 , 46.7 , 46.54
2000 4 46.54 93.41 , 46.7 , 46.54
2001 1 92.26 256.7 , 118.38 , 92.26 , 47.24
2001 2 118.38 256.7 , 118.38 , 92.26 , 47.24
2001 3 47.24 256.7 , 118.38 , 92.26 , 47.24
2001 4 256.7 256.7 , 118.38 , 92.26 , 47.24
14 rows selected.
함수 가 서술 어 앞 에 미 치 는 영향 을 분석 하 다.
분석 함수 의 보 기 를 사용 하면 보기 전 추 에 영향 을 줄 수 있 습 니 다. 분석 함수 의 결 과 는 줄 을 넘 어 참조 할 수 있 기 때문에 데이터 원본 을 편집 하면 결과 가 다 를 수 있 습 니 다.
SH@ prod> create or replace view max_5_weeks_vw as
2 select country , product , region , year , week , sale ,
3 max(sale) over(
4 partition by product , country , region , year order by year , week
5 rows between 2 preceding and 2 following ) max_weeks_5
6 from sales_fact ;
View created.
SH@ prod> select year , week , sale , max_weeks_5 from max_5_weeks_vw
2 where country in ('Australia' ) and product = 'Xtend Memory'
3 and region = 'Australia' and year = 2000 and week < 14
4 order by year , week ;
YEAR WEEK SALE MAX_WEEKS_5
---------- ---------- ---------- -----------
2000 1 46.7 93.41
2000 3 93.41 93.41
2000 4 46.54 93.41
2000 5 46.7 93.41
2000 7 70.8 93.74
2000 8 46.54 93.74
2000 11 93.74 117.5
2000 12 46.54 117.67
2000 13 117.5 117.67
9 rows selected.
SH@ prod> explain plan for
2 select year , week , sale , max_weeks_5 from max_5_weeks_vw
3 where country in ('Australia' ) and product = 'Xtend Memory'
4 and region = 'Australia' and year = 2000 and week < 14
5 order by year , week ;
Explained.
SH@ prod> select * from table(dbms_xplan.display());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------
Plan hash value: 4167461139
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 90 | 5220 | 310 (1)| 00:00:04 |
|* 1 | VIEW | MAX_5_WEEKS_VW | 90 | 5220 | 310 (1)| 00:00:04 |
| 2 | WINDOW SORT | | 90 | 9450 | 310 (1)| 00:00:04 |
|* 3 | TABLE ACCESS FULL| SALES_FACT | 90 | 9450 | 309 (1)| 00:00:04 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("WEEK"<14)
3 - filter("COUNTRY"='Australia' AND "PRODUCT"='Xtend Memory' AND
"REGION"='Australia' AND "YEAR"=2000)
Note
-----
- dynamic sampling used for this statement (level=2)
21 rows selected.
분석 함수 가 없 는 보 기 를 비교 합 니 다.서술 어 를 보기 에 직접 밀어 넣 습 니 다.
SH@ prod> create or replace view max_5_weeks_vw1 as
2 select country , product , region , year , week , sale
3 from sales_fact ;
View created.
SH@ prod> explain plan for
2 select year , week , sale from max_5_weeks_vw1
3 where country in ('Australia' ) and product = 'Xtend Memory'
4 and region = 'Australia' and year = 2000 and week < 14
5 order by year , week ;
Explained.
SH@ prod> select * from table(dbms_xplan.display());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1978576542
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 105 | 310 (1)| 00:00:04 |
| 1 | SORT ORDER BY | | 1 | 105 | 310 (1)| 00:00:04 |
|* 2 | TABLE ACCESS FULL| SALES_FACT | 1 | 105 | 309 (1)| 00:00:04 |
---------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("COUNTRY"='Australia' AND "PRODUCT"='Xtend Memory' AND
"REGION"='Australia' AND "YEAR"=2000 AND "WEEK"<14)
Note
-----
- dynamic sampling used for this statement (level=2)
19 rows selected.
분석 함 수 는 동적 SQL 에 사 용 됩 니 다.
SH@ prod> create or replace procedure analytic_dynamic_prc ( part_col_string varchar2 , v_country varchar2 , v_product varchar2 )
2 is
3 type numtab is table of number(18 , 2) index by binary_integer ;
4 l_year numtab ;
5 l_week numtab ;
6 l_sale numtab ;
7 l_rank numtab ;
8 l_sql_string varchar2(512) ;
9 begin
10 l_sql_string := 'select * from ( select year , week , sale , rank() over( partition by ' || part_col_string
11 || ' order by sale desc ) sales_rank from sales_fact where country in ('
12 || chr(39) || v_country || chr(39)
13 || ' ) and product = ' || chr(39) || v_product || chr(39)
14 || 'order by product , country , year , week ) where sales_rank <= 10 order by 1,4' ;
15 execute immediate l_sql_string bulk collect into l_year , l_week , l_sale , l_rank ;
16 for i in 1..l_year.count loop
17 dbms_output.put_line( l_year(i) || ' | ' || l_week(i) || ' | ' || l_sale(i) || ' | ' || l_rank(i) ) ;
18 end loop ;
19 end ;
20 /
Procedure created.
SH@ prod> exec analytic_dynamic_prc('product , country , region' , 'Australia' , 'Xtend Memory' ) ;
1998 | 48 | 172.56 | 9
2000 | 46 | 246.74 | 3
2000 | 21 | 187.48 | 5
2000 | 43 | 179.12 | 7
2000 | 34 | 178.52 | 8
2001 | 16 | 278.44 | 1
2001 | 4 | 256.7 | 2
2001 | 21 | 233.7 | 4
2001 | 48 | 182.96 | 6
2001 | 30 | 162.91 | 10
2001 | 14 | 162.91 | 10
PL/SQL procedure successfully completed.
함 수 를 분석 하 는 "끼 워 넣 기"
분석 함 수 는 직접 끼 워 넣 을 수 없고 하위 조 회 를 통 해 이 루어 질 수 있 습 니 다.
select year , week , top_sale_year ,
lag( top_sale_year ) over ( order by year desc ) prev_top_sale_yer
from (
select distinct
first_value(year) over ( MAX , 。
partition by product , country , region , year
order by sale desc
rows between unbounded preceding and unbounded following ) year ,
first_value(week) over (
partition by product , country , region , year
order by sale desc
rows between unbounded preceding and unbounded following ) week ,
first_value(sale) over (
partition by product , country , region , year
order by sale desc
rows between unbounded preceding and unbounded following ) top_sale_year
from sales_fact
where country in ('Australia') and product = 'Xtend Memory' )
order by year , week ;
실행 결과.
SH@ prod> select year , week , top_sale_year ,
2 lag( top_sale_year ) over ( order by year desc ) prev_top_sale_yer
3 from (
4 select distinct
5 first_value(year) over (
6 partition by product , country , region , year
7 order by sale desc
8 rows between unbounded preceding and unbounded following ) year ,
9 first_value(week) over (
10 partition by product , country , region , year
11 order by sale desc
12 rows between unbounded preceding and unbounded following ) week ,
13 first_value(sale) over (
14 partition by product , country , region , year
15 order by sale desc
16 rows between unbounded preceding and unbounded following ) top_sale_year
17 from sales_fact
18 where country in ('Australia') and product = 'Xtend Memory' )
19 order by year , week ;
YEAR WEEK TOP_SALE_YEAR PREV_TOP_SALE_YER
---------- ---------- ------------- -----------------
1998 48 172.56 148.12
1999 17 148.12 246.74
2000 46 246.74 278.44
2001 16 278.44
분석 함수 의 병렬
이전 절 에 있 는 문장의 실행 계획 을 보십시오.
SH@ prod> explain plan for
2 select year , week , top_sale_year ,
3 lag( top_sale_year ) over ( order by year desc ) prev_top_sale_yer
4 from (
5 select distinct
6 first_value(year) over (
7 partition by product , country , region , year
8 order by sale desc
9 rows between unbounded preceding and unbounded following ) year ,
10 first_value(week) over (
11 partition by product , country , region , year
12 order by sale desc
13 rows between unbounded preceding and unbounded following ) week ,
14 first_value(sale) over (
15 partition by product , country , region , year
16 order by sale desc
17 rows between unbounded preceding and unbounded following ) top_sale_year
18 from sales_fact
19 where country in ('Australia') and product = 'Xtend Memory' )
20 order by year , week ;
Explained.
SH@ prod> select * from table(dbms_xplan.display());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2124823565
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 197 | 7683 | 313 (2)| 00:00:04 |
| 1 | SORT ORDER BY | | 197 | 7683 | 313 (2)| 00:00:04 |
| 2 | WINDOW SORT | | 197 | 7683 | 313 (2)| 00:00:04 |
| 3 | VIEW | | 197 | 7683 | 311 (1)| 00:00:04 |
| 4 | HASH UNIQUE | | 197 | 20685 | 311 (1)| 00:00:04 |
| 5 | WINDOW SORT | | 197 | 20685 | 311 (1)| 00:00:04 |
|* 6 | TABLE ACCESS FULL| SALES_FACT | 197 | 20685 | 309 (1)| 00:00:04 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
6 - filter("COUNTRY"='Australia' AND "PRODUCT"='Xtend Memory')
Note
-----
- dynamic sampling used for this statement (level=2)
22 rows selected.
( DISTINCT HASH UNIQUE )
위의 문장 에 병렬 힌트 를 추가 합 니 다.
SH@ prod> explain plan for
2 select /*+ parallel(3)*/ year , week , top_sale_year ,
3 lag( top_sale_year ) over ( order by year desc ) prev_top_sale_yer
4 from (
5 select distinct
6 first_value(year) over (
7 partition by product , country , region , year
8 order by sale desc
9 rows between unbounded preceding and unbounded following ) year ,
10 first_value(week) over (
11 partition by product , country , region , year
12 order by sale desc
13 rows between unbounded preceding and unbounded following ) week ,
14 first_value(sale) over (
15 partition by product , country , region , year
16 order by sale desc
17 rows between unbounded preceding and unbounded following ) top_sale_year
18 from sales_fact
19 where country in ('Australia') and product = 'Xtend Memory' )
20 order by year , week ;
Explained.
SH@ prod> set linesize 180
SH@ prod> select * from table(dbms_xplan.display());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2880616722
----------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | TQ |IN-OUT| PQ Distrib |
----------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 197 | 7683 | 119 (5)| 00:00:02 | | | |
| 1 | SORT ORDER BY | | 197 | 7683 | 119 (5)| 00:00:02 | | | |
| 2 | WINDOW BUFFER | | 197 | 7683 | 119 (5)| 00:00:02 | | | |
| 3 | PX COORDINATOR | | | | | | | | |
| 4 | PX SEND QC (ORDER) | :TQ10003 | 197 | 7683 | 119 (5)| 00:00:02 | Q1,03 | P->S | QC (ORDER) |
| 5 | SORT ORDER BY | | 197 | 7683 | 119 (5)| 00:00:02 | Q1,03 | PCWP | |
| 6 | PX RECEIVE | | 197 | 7683 | 117 (3)| 00:00:02 | Q1,03 | PCWP | |
| 7 | PX SEND RANGE | :TQ10002 | 197 | 7683 | 117 (3)| 00:00:02 | Q1,02 | P->P | RANGE |
| 8 | VIEW | | 197 | 7683 | 117 (3)| 00:00:02 | Q1,02 | PCWP | |
| 9 | HASH UNIQUE | | 197 | 20685 | 117 (3)| 00:00:02 | Q1,02 | PCWP | |
| 10 | PX RECEIVE | | 197 | 20685 | 117 (3)| 00:00:02 | Q1,02 | PCWP | |
| 11 | PX SEND HASH | :TQ10001 | 197 | 20685 | 117 (3)| 00:00:02 | Q1,01 | P->P | HASH |
| 12 | WINDOW SORT | | 197 | 20685 | 117 (3)| 00:00:02 | Q1,01 | PCWP | |
| 13 | PX RECEIVE | | 197 | 20685 | 114 (0)| 00:00:02 | Q1,01 | PCWP | |
| 14 | PX SEND HASH | :TQ10000 | 197 | 20685 | 114 (0)| 00:00:02 | Q1,00 | P->P | HASH |
| 15 | PX BLOCK ITERATOR | | 197 | 20685 | 114 (0)| 00:00:02 | Q1,00 | PCWC | |
|* 16 | TABLE ACCESS FULL| SALES_FACT | 197 | 20685 | 114 (0)| 00:00:02 | Q1,00 | PCWP | |
----------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
16 - filter("COUNTRY"='Australia' AND "PRODUCT"='Xtend Memory')
Note
-----
- dynamic sampling used for this statement (level=2)
- Degree of Parallelism is 3 because of hint
33 rows selected.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Redash를 사용할 때 몰랐던 SQL을 쓰는 법을 배웠습니다.최근 redash에서 sql을 쓸 기회가 많고, 이런 쓰는 방법이 있었는지와 sql에 대해 공부를 다시하고 있기 때문에 배운 것을 여기에 씁니다. Redash란? 월별로 데이터를 표시하고 싶습니다 주별로 데이터를 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.