REGR_COUNT

REGR_COUNT is an SQL aggregate function that calculates the number of non-null pairs of values in two specific columns within a group of rows in a SQL table. This function is typically used in conjunction with regression analysis commands.

REGR_COUNT(Y, X)

  • y: This is the dependent variable in the regression equation. It is the variable whose values will be predicted or explained in relation with another variable, referred to as X in this function.
  • x: This is the independent variable in the regression equation. It is the variable that is used to predict or explain changes in the dependent variable, Y, in this function. This is the variable that is manipulated to see if it has an effect on Y.

Example

SELECT REGR_COUNT(salary, commission_pct) OVER () AS regr_count
FROM employees;

Output

REGR_COUNT
----------------
14

Explanation

The REGR_COUNT function returns the number of non-null pairs of numbers in a group. In this example, it returns the count of non-null pairs of salary and commission_pct from the employees table.

The basic signature for the REGR_COUNT function in PostgreSQL is as follows:REGR_COUNT(Y, X)

  • y: This parameter represents the dependent variable in the regression equation. It can be any numeric data type that PostgreSQL supports. The function calculates the number of pairs (Y,X) where Y is not null.
  • x: This parameter signifies the independent variable in the regression line equation. Similar to Y, it can also be any numeric data type recognized by PostgreSQL. The function counts the pairs (Y,X) where X is not null.

Example

SELECT REGR_COUNT(weight, height) OVER() FROM humans;

Output

+---------------------+
| regr_count |
+---------------------+
| 100 |
+---------------------+

Explanation

In the above example, the REGR_COUNT function is used to count the pairs of numbers without NULL values in the “weight” and “height” columns of the “humans” table. The OVER() function specifies that the count should be done over the entire set of rows. If there are 100 non-NULL pairs of numbers, it will return 100.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.