Function to Calculate Median in SQL Server

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

    Simple, fast, accurate

    SELECT x.Amount 
    FROM   (SELECT amount, 
                   Count(1) OVER (partition BY 'A')        AS TotalRows, 
                   Row_number() OVER (ORDER BY Amount ASC) AS AmountOrder 
            FROM   facttransaction ft) x 
    WHERE  x.AmountOrder = Round(x.TotalRows / 2.0, 0)  
    

提交回复
热议问题