count_if mängdfunktion

Gäller för:check marked yes Databricks SQL check marked yes Databricks Runtime

Returnerar antalet sanna värden för gruppen i expr.

Syntax

count_if ( [ALL | DISTINCT] expr ) [ FILTER ( WHERE cond ) ]

Den här funktionen kan också anropas som en fönsterfunktion med hjälp av OVER -satsen.

Argument

  • expr: Ett BOOLESKT uttryck.
  • cond: Ett valfritt booleskt uttryck som filtrerar de rader som används för aggregering.

Returer

En BIGINT.

count_if(expr) FILTER(WHERE cond) motsvarar count_if(expr AND cond).

Om DISTINCT anges räknas endast unika rader.

Exempel

> SELECT count_if(col % 2 = 0) FROM VALUES (NULL), (0), (1), (2), (2), (3) AS tab(col);
 3

> SELECT count_if(DISTINCT col % 2 = 0) FROM VALUES (NULL), (0), (1), (2), (2), (3) AS tab(col);
 2

> SELECT count_if(col IS NULL) FROM VALUES (NULL), (0), (1), (2), (3) AS tab(col);
 1