Function to Calculate Median in SQL Server

前端 未结 30 2830
孤独总比滥情好
孤独总比滥情好 2020-11-22 04:03

According to MSDN, Median is not available as an aggregate function in Transact-SQL. However, I would like to find out whether it is possible to create this functionality (u

30条回答
  •  臣服心动
    2020-11-22 04:25

    For a continuous variable/measure 'col1' from 'table1'

    select col1  
    from
        (select top 50 percent col1, 
        ROW_NUMBER() OVER(ORDER BY col1 ASC) AS Rowa,
        ROW_NUMBER() OVER(ORDER BY col1 DESC) AS Rowd
        from table1 ) tmp
    where tmp.Rowa = tmp.Rowd
    

提交回复
热议问题