Updated: 6 August 2010
Use MEDIAN to return the median of the given numbers. The median is the number in the middle of a set of numbers.
Syntax
SELECT [wctStatistics].[wct].[MEDIAN] (
<@Values_TableName, nvarchar(4000),>
,<@ColumnName, nvarchar(4000),>
,<@GroupedColumnName, nvarchar(4000),>
,<@GroupedColumnValue, sql_variant,>)
Arguments
@Values_TableName
the name, as text, of the table or view that contains the values to be used in the MEDIAN calculation.
@ColumnName
the name, as text, of the column in the table or view specified by @Values_TableName that contains the values to be used in the MEDIAN calculation.
@GroupedColumnName
the name, as text, of the column in the table or view specified by @Values_TableName which will be used for grouping the results.
@GroupedColumnValue
the column value to do the grouping on.
Return Types
float
Remarks
· If there is an even number of numbers in the set, then MEDIAN calculates the average of the two numbers in the middle. See the second formula in the example.
· MEDIAN only includes values of type float or of a type that can be implicitly converted to float. All other values and NULL are excluded.
· For more complicated queries consider using the MEDIAN_q function.
· No GROUP BY is required for this function even though it produces aggregated results.
Examples
CREATE TABLE #m1(
[num] [float] NOT NULL
)
INSERT INTO #m1 VALUES (1)
INSERT INTO #m1 VALUES (2)
INSERT INTO #m1 VALUES (4)
INSERT INTO #m1 VALUES (7)
INSERT INTO #m1 VALUES (8)
INSERT INTO #m1 VALUES (9)
INSERT INTO #m1 VALUES (10)
INSERT INTO #m1 VALUES (12)
select wct.MEDIAN('#m1','num','',NULL)
This produces the following result
----------------------
7.5
(1 row(s) affected)