Function to Calculate Median in SQL Server

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

    For newbies like myself who are learning the very basics, I personally find this example easier to follow, as it is easier to understand exactly what's happening and where median values are coming from...

    select
     ( max(a.[Value1]) + min(a.[Value1]) ) / 2 as [Median Value1]
    ,( max(a.[Value2]) + min(a.[Value2]) ) / 2 as [Median Value2]
    
    from (select
        datediff(dd,startdate,enddate) as [Value1]
        ,xxxxxxxxxxxxxx as [Value2]
         from dbo.table1
         )a
    

    In absolute awe of some of the codes above though!!!

提交回复
热议问题