Function to Calculate Median in SQL Server

前端 未结 30 3123
孤独总比滥情好
孤独总比滥情好 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:16

    In a UDF, write:

     Select Top 1 medianSortColumn from Table T
      Where (Select Count(*) from Table
             Where MedianSortColumn <
               (Select Count(*) From Table) / 2)
      Order By medianSortColumn
    

提交回复
热议问题