Freigeben über


count_if-Aggregatfunktion

Gilt für:check marked yes Databricks SQL check marked yes Databricks Runtime

Gibt die Anzahl der TRUE-Werte für die Gruppe in expr zurück.

Syntax

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

Diese Funktion kann auch mithilfe der OVER-Klausel als Fensterfunktion aufgerufen werden.

Argumente

  • expr: Ein BOOLESCHER Ausdruck
  • cond: Ein optionaler boolescher Ausdruck, der die für die Aggregation verwendeten Zeilen filtert.

Gibt zurück

Ein BIGINT.

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

Wenn DISTINCT angegeben wird, werden nur eindeutige Zeilen gezählt.

Beispiele

> 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