Skip to main content
Returns the sample covariance of two expressions. This function is supported from v2.5.10. It can also be used as a window function.

Syntax

Parameters

expr1 and expr2 must evaluate to TINYINT, SMALLINT, INT, BIGINT, LARGEINT, FLOAT, DOUBLE, or DECIMAL. If expr1 and expr2 are table columns, this function calculates the sample covariance of these two columns.

Return value

Returns a DOUBLE value. The formula is as follows, where n represents the row count of the table: covar_samp formula

Usage notes

  • A data row is counted only when the two columns in this row are non-null values. Otherwise, this data row is eliminated from the result.
  • 0 is returned if n is 1.
  • NULL is returned in any input is NULL.

Examples

Suppose table agg has the following data:
Calculate the sample covariance of the k and `v“ columns: