Updated on 2026-11-06 GMT+08:00

covar_samp

Function

The covar_samp function is used to return the sample covariance of two numeric columns.

Syntax

covar_samp(col1, col2)

Parameters

Parameter

Mandatory

Description

col1

Yes

A column with numeric data type. Other types return NULL.

col2

Yes

A column with numeric data type. Other types return NULL.

Return Values

Returns a value of type DOUBLE.

Examples

  • Calculate the sample covariance between all items inventory and price. Example command:
    select covar_samp(items,price) from warehouse; 

    Result:

    _c0         
    1.242355   
  • Used with GROUP BY to group all items by warehouse (warehouseId) and calculate the sample covariance between items inventory and price within the same group. Example command:
    select warehouseId, covar_samp(items,price) from warehouse group by warehouseId; 

    Result:

    warehouseId _c1         
    city1    1.03124    
    city2    1.03344    
    city3    1.33425