Count 집계 함수
구문
%%{init: {
'theme': 'base',
'flowchart': { 'padding': '7', 'nodeSpacing': '20', 'rankSpacing': '20' },
'themeVariables': {
'fontSize': '11px',
'fontFamily': 'Arial'
}
}}%%
flowchart TD
countAggregateFunction_start((START))
countAggregateFunction_start --> countAggregateFunction_0_0[COUNT]:::quoted
countAggregateFunction_0_0 --> countAggregateFunction_0_1["("]:::quoted
countAggregateFunction_0_1 --> countAggregateFunction_0_2[DISTINCT]:::quoted
countAggregateFunction_0_1 --> countAggregateFunction_0_2a[ALL]:::quoted
countAggregateFunction_0_2a --> countAggregateFunction_0_3
countAggregateFunction_0_1 --> countAggregateFunction_0_3[<a href="Invantive UniversalSQL/Grammar/Query Statement/Select Statement#part" class="internal-link">part</a>]
countAggregateFunction_0_2 --> countAggregateFunction_0_3[<a href="Invantive UniversalSQL/Grammar/Query Statement/Select Statement#part" class="internal-link">part</a>]
countAggregateFunction_0_3 --> countAggregateFunction_0_4[")"]:::quoted
countAggregateFunction_0_4 --> countAggregateFunction_end((END))
목적
값 그룹에서 값의 개수를 구하는 그룹 함수입니다. count(*)는 null 값만 포함된 행을 포함하여 모든 행을 셉니다. count(expression)는 식이 null로 평가되지 않는 행을 셉니다. count(distinct expression)는 null이 아닌 고유 값을 셉니다. ALL이(가) 기본값이며, 각 값은 발생한 횟수만큼 계산됩니다. ALL은(는) 릴리스 28.0부터 사용할 수 있습니다.
예시
다음 예제는 고객 다섯 명으로 이루어진 고객 기반에서 고객 수를 셉니다:
select count(*)
from csvtable
( passing 'Acme Industries#Manufacturing#12500#50000|Bergmann GmbH#Manufacturing#0#25000|Carlson Ltd#Wholesale#2500#10000|Dubois SA#Wholesale#7500#10000|Everest BV#Services##5000'
row delimiter '|'
column delimiter '#'
columns name varchar2 position next
, segment varchar2 position next
, balance number position next
, credit_limit number position next
) cus
-------------------
5
다음 예제는 세 가지 변형의 차이를 보여줍니다. 고객 다섯 명 중 네 명은 미결제 잔액이 있으며(Everest BV의 잔액은 null입니다), 이들은 고유 세그먼트 세 개에 걸쳐 분포합니다:
select count(*)
, count(cus.balance)
, count(distinct cus.segment)
from csvtable
( passing 'Acme Industries#Manufacturing#12500#50000|Bergmann GmbH#Manufacturing#0#25000|Carlson Ltd#Wholesale#2500#10000|Dubois SA#Wholesale#7500#10000|Everest BV#Services##5000'
row delimiter '|'
column delimiter '#'
columns name varchar2 position next
, segment varchar2 position next
, balance number position next
, credit_limit number position next
) cus
-------------------
5 4 3
다음 예제는 고객별 담당자 수를 셉니다. 예를 들어 청구 담당자가 없는 고객을 찾기 위해서입니다:
select cnt.customer_name
, count(*)
from csvtable
( passing 'Acme Industries#Alice Johnson#CFO|Acme Industries#Tom Peters#Accounts Payable|Bergmann GmbH#Erika Weber#Controller|Carlson Ltd#James Smith#CFO|Dubois SA#Marie Dupont#Accounts Payable'
row delimiter '|'
column delimiter '#'
columns customer_name varchar2 position next
, contact_name varchar2 position next
, role varchar2 position next
) cnt
group
by cnt.customer_name
order
by cnt.customer_name
-------------------
Acme Industries 2
Bergmann GmbH 1
Carlson Ltd 1
Dubois SA 1