Logo

HARMEAN_q

Updated 2023-10-23 19:46:43.703000

Syntax

SELECT [westclintech].[wct].[HARMEAN_q] (
   <@Values_RangeQuery, nvarchar(4000),>)

Description

Use the scalar function HARMEAN_q to calculate the harmonic mean of a dataset containing positive numbers. The harmonic mean is the reciprocal of the arithmetic mean of reciprocals. The equation for harmonic mean is:

\frac{1}{H} = \frac{1}{n}\sum_{i=1}^n\frac{1}{x_I}

Arguments

@Values_RangeQuery

the select statement, as text, used to determine the values to be used in the HARMEAN_q calculation.

Return Type

float

Remarks

If any values in the dataset are less than or equal to zero, HARMEAN_q will return an error.

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

Examples

CREATE TABLE #h1
(
    [num] [float] NOT NULL
);
insert into #h1
Values
(4.6);
insert into #h1
Values
(5.7);
insert into #h1
Values
(8.3);
insert into #h1
Values
(7.29);
insert into #h1
Values
(10.965);
insert into #h1
Values
(4.166667);
insert into #h1
Values
(3.14159265358979);
select wct.HARMEAN_q('Select num from #h1');

This produces the following result

----------------------
      5.38463536083465