Documentación de Invantive
Foros de Soporte de InvantiveIniciar Sesión / Registrarse

Función de agregado Count

Sintaxis

%%{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))

Propósito

Función de grupo para contar el número de valores de un grupo de valores. count(*) cuenta todas las filas, incluidas las filas con solo valores null. count(expression) cuenta las filas para las que la expresión no se evalúa como null. count(distinct expression) cuenta los valores únicos distintos de null. Con ALL, el valor por defecto, cada valor cuenta tantas veces como aparece; ALL se acepta desde la versión 28.0.

Ejemplos

El siguiente ejemplo cuenta el número de clientes en una base de cinco clientes:

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

El siguiente ejemplo muestra la diferencia entre las tres variantes: cinco clientes, de los cuales cuatro tienen un saldo pendiente (el saldo de Everest BV es null), repartidos en tres segmentos únicos:

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

El siguiente ejemplo cuenta el número de personas de contacto por cliente, por ejemplo para encontrar clientes sin contacto de facturación:

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