Categories:
Aggregate Functions (Linear Regression) , Window Functions

REGR_AVGX

Returns the average of the independent variable for non-null pairs in a group, where x is the independent variable and y is the dependent variable.

Syntax

REGR_AVGX(y, x)

Usage Notes

  • DISTINCT is not supported for this function.
  • In order for a row to be included in the average, BOTH the x and y values must be non-NULL.

Examples

CREATE OR REPLACE TABLE aggr(k int, v decimal(10,2), v2 decimal(10, 2));
INSERT INTO aggr VALUES(1, 10, NULL);
INSERT INTO aggr VALUES(2, 10, 11), (2, 20, 22), (2, 25, NULL), (2, 30, 35);
SELECT k, REGR_AVGX(v, v2) FROM aggr GROUP BY k;

---+------------------+
 k | regr_avgx(v, v2) |
---+------------------+
 1 | [NULL]           |
 2 | 22.666666667     |
---+------------------+