zoukankan      html  css  js  c++  java
  • [转]How to calculate Median in SQL Server

    How to calculate Median in SQL Server

    Nothing earth-shattering here, I was just helping out a colleague with this so I thought I'd post up the example I gave him.

    -- sample table:
    create table People
    (
        Person varchar(1) primary key,
        City varchar(10),
        Age int
    )

    go

    -- with some sample data:

    insert into People
    select 'A','Boston',23 union all  -- odd #
    select 'B','Boston',43 union all
    select 'C','Boston',29 union all

    select 'D','Chicago',15 union all -- single #

    select 'E','NY',12 union all  -- even #
    select 'F','NY',55 union all
    select 'G','NY',57 union all
    select 'H','NY',61


    go

    -- here's our query, showing median age per city:

    select city,
        AVG(age) as MedianAge
    from
    (
        select City, Person, Age,
            ROW_NUMBER() over (partition by City order by Age ASC) as AgeRank,
            COUNT(*) over (partition by City) as CityCount
        from
            People
    ) x
    where
        x.AgeRank in (x.CityCount/2+1, (x.CityCount+1)/2)   
    group by
        x.City    
       

    go

    -- clean it all up
    drop table People

    And here's the result:


    city       MedianAge
    ---------- -----------
    Boston     29
    Chicago    15
    NY         56

    (3 row(s) affected)

    Simply remove "City" from the SELECT clause and the GROUP BY clause to get the median age for all. 

    There may be more efficient tricks out there, but this is certainly the shortest and simplest technique I am aware of.

  • 相关阅读:
    python冲刺(5)列表声称式
    python冲刺(4)切片 等
    python冲刺(3)函数 等
    python冲刺(2)
    python冲刺(1)
    redis初步(1)
    php连接Oracle的时候遇到的编码集问题
    redis初步
    php 命名空间
    指向字符数组的指针与指向整型数组的指针
  • 原文地址:https://www.cnblogs.com/gzhu/p/2521688.html
Copyright © 2011-2022 走看看