Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Median values in T-SQL

For even rows, the formula for median is (104.5 + 108)/2 for the table below and for odd rows it is 108 for table below:

Total       Total

100         100
101         101
104.5       104.5
108         108
108.3       108.3
112         112
            114

I wrote this query, and it is calculating the correct median when the number of rows are odd:

WITH    a AS ( SELECT   Total ,
                        ROW_NUMBER() OVER ( ORDER BY CAST(Total AS FLOAT) ASC ) rownumber
               FROM     [Table] A
             ),
        b AS ( SELECT TOP 2
                        Total ,
                        isodd
               FROM     ( SELECT TOP 50 PERCENT
                                    Total ,
                                    rownumber % 2 isodd
                          FROM      a
                          ORDER BY  CAST(Total AS FLOAT) ASC
                        ) a
               ORDER BY CAST(total AS FLOAT) DESC
             )
    SELECT  *
    FROM    b

What is the general T-SQL query to find the median in both situations? Like when the number of rows are odd and also when the number of rows is even?

Could my query be twisted so that it can work for the median in both even and odd number of rows situations?

like image 556
6 revs, 3 users 89% Avatar asked Aug 29 '11 08:08

6 revs, 3 users 89%


People also ask

How do you calculate median in SQL?

To get the median we have to use PERCENTILE_CONT(0.5). If you want to define a specific set of rows grouped to get the median, then use the OVER (PARTITION BY) clause. Here I've used PARTITION BY on the column OrderID so as to find the median of unit prices for the order ids.

Can you take the median in SQL?

As mentioned, at the time of writing this article, there is no median function in SQL Server. However, we can use the PERCENTILE_CONT function to achieve the same functionality. The function returns the value ranked at a specific percent for a defined set of values.

How do you find the median of a group in SQL?

Alternative: using a functionSELECT tag, median(tag) FROM median; -- my test table is 'median' too... This query will be "better": select tag, median(tag) from (select distinct tag from median) t; That's all I can do!


1 Answers

I wrote a blog about Mean, Median and Mode a couple years ago. I encourage you to read it.

Calculating Mean, Median, and Mode with SQL Server

SELECT ((
        SELECT TOP 1 Total
        FROM   (
                SELECT  TOP 50 PERCENT Total
                FROM    [TABLE] A
                WHERE   Total IS NOT NULL
                ORDER BY Total
                ) AS A
        ORDER BY Total DESC) +
        (
        SELECT TOP 1 Total
        FROM   (
                SELECT  TOP 50 PERCENT Total
                FROM    [TABLE] A
                WHERE   Total IS NOT NULL
                ORDER BY Total DESC
                ) AS A
        ORDER BY Total ASC)) / 2
like image 74
George Mastros Avatar answered Oct 20 '22 00:10

George Mastros