Logo

SLOPE_q

Updated 2023-11-02 12:57:28.603000

Syntax

SELECT [westclintech].[wct].[SLOPE_q] (
   <@Known_y_Known_x_RangeQuery, nvarchar(4000),>)

Description

Use the scalar function SLOPE_q to calculate the slope of the linear regression through the data points in the known x-values and y-values. The slope is the vertical distance divided by the horizontal distance between any two points on the line, which is the rate of change along the regression line. The equation for the slope of the regression line is:

b=\frac{\sum(x-\bar{x})(y-\bar{y})}{\sum(x-\bar{x})^2}

Arguments

@Known_y_Known_x_RangeQuery

the select statement, as text, used to determine the known y- and x-values to be used in the SLOPE_q function.

Return Type

float

Remarks

If the number of y-data points is not equal to the number of x-data points, SLOPE_q will return an error.

No GROUP BY is required for this function even though it produces aggregated results.

Examples

CREATE TABLE #i1

(

    [y] [float] NOT NULL,

    [x] [float] NOT NULL

);

INSERT INTO #i1

VALUES

(-4.5, -1);

INSERT INTO #i1

VALUES

(0, 2);

INSERT INTO #i1

VALUES

(-9, -4);

INSERT INTO #i1

VALUES

(4.5, 5);

INSERT INTO #i1

VALUES

(-13.5, -7);

SELECT wct.SLOPE_q('SELECT y, x from #i1');

This produces the following result

{"columns":[{"field":"column 1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"column 1":"1.5"}]}