![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
You can create custom aggregate with CLR:http://msdn.microsoft.com/en-us/library/ms131057.aspx But if you are using SQL Server 2008 you can pass table-valued parametersto functions and stored procedures and calculate median that way.http://msdn.microsoft.com/en-us/library/bb510489.aspx -- Plamen Ratchevhttp://www.SQLStudio.com |
#4
| |||
| |||
|
|
There is a good article at ASPFAQ on how to calculate median in a SQL table. (http://databases.aspfaq.com/database...calculate-the- median-in-a-table.html). I would like to either turn that into a stored procedure, or ideally turn it into a custom aggregate function do I could use it with a GROUP BY clause. There are details and caveats in the article, but the gist of the SQL is: SELECT AVG(splunge) FROM ( SELECT splunge FROM ( SELECT TOP 1 splunge = splunge * 1.0 FROM ( SELECT TOP 50 PERCENT splunge FROM blat ORDER BY splunge ) sub_a ORDER BY 1 DESC ) sub_1 UNION ALL SELECT splunge FROM ( SELECT TOP 1 splunge = splunge * 1.0 FROM ( SELECT TOP 50 PERCENT splunge FROM blat ORDER BY splunge DESC ) sub_b ORDER BY 1 ) sub_2 ) median Is it possible to create custom aggregate functions in SQL? If not, is it possible to pass an array into an SP so the SP could return the median? I haven't found any direct way of doing either of these. |
![]() |
| Thread Tools | |
| Display Modes | |
| |