Count Aggregate Function
Syntax
%%{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))
Purpose
Group function to find the number of values from a group of values. count(*) counts all rows, including rows with solely null values. count(expression) counts the rows for which the expression does not evaluate to null. count(distinct expression) counts the unique non-null values. With ALL, the default, every value counts as often as it occurs; ALL is accepted from release 28.0.
Examples
The following example counts the number of customers in a customer base of five customers:
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
The following example shows the difference between the three variants: five customers, of which four have an outstanding balance (the balance of Everest BV is null), spread over three unique segments:
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
The following example counts the number of contact persons per customer, for instance to find customers without a billing contact:
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