Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how to create aggregate function

Author  Topic 

Asi
Starting Member

3 Posts

Posted - 2009-04-26 : 13:45:07
Hi

I want to create the statistical "Median" function and to use it in the same way that I use all aggregate functions such as sum(), avg() etc..
I want to simply call it with a column name (e.g: "Median (Col)" ) and it will return a median value. It will be able to calculate the median of all column values and on groups of rows using the GROUP BY clause as all aggregate functions do.

At first I created the Median function on a specific table as described in the following article (just to practice the algorithm):
[url]
http://www.sqlmag.com/articles/index.cfm?articleid=49827
[/url]

I created the following view to work on:

IF OBJECT_ID('dbo.VOrders') IS NOT NULL
DROP VIEW dbo.VOrders;
GO
CREATE VIEW dbo.VOrders
AS
SELECT O.OrderID, O.OrderDate, O.CustomerID, O.EmployeeID, O.ShipVia,
SUM(OD.Quantity) AS Qty,
CAST(SUM(OD.Quantity * UnitPrice * (1 - Discount)) AS DECIMAL(12, 2)) AS Value
FROM dbo.Orders AS O
JOIN [Order Details] AS OD
ON O.OrderID = OD.OrderID
GROUP BY O.OrderID, O.OrderDate, O.CustomerID, O.EmployeeID, O.ShipVia
GO


And then I tried the 2 ways to calculate the Median as described in the article.

SQL Server 2000 way:

SELECT EmployeeID,
(
(SELECT MAX(Value) FROM
(SELECT TOP 50 PERCENT Value FROM dbo.VOrders AS O1
WHERE O1.EmployeeID = E.EmployeeID
ORDER BY Value) AS H1)
+
(SELECT MIN(Value) FROM
(SELECT TOP 50 PERCENT Value FROM dbo.VOrders AS O2
WHERE O2.EmployeeID = E.EmployeeID
ORDER BY Value DESC) AS H2)
) / 2 AS Median
FROM dbo.Employees AS E
order by E.EmployeeID



SQL Server 2005 Way:

WITH OrdersRN AS
(
SELECT EmployeeID, Value,
ROW_NUMBER() OVER(PARTITION BY EmployeeID ORDER BY Value) AS RowNum,
COUNT(*) OVER(PARTITION BY EmployeeID) AS Cnt
FROM dbo.VOrders
)
SELECT EmployeeID, AVG(Value) AS Median
FROM OrdersRN
WHERE RowNum IN((Cnt + 1) / 2, (Cnt + 2) / 2)
GROUP BY EmployeeID;


Both Algorithms work OK.
After checking those algorithms I tried to convert the code in a way that the function will accept parameter (col name) so I could use it as every aggregate function. I want to be able to call it as follows:

select EmployeeID, Median (Value)
FROM VOrders
GROUP BY EmployeeID

Then I started to run into problems. First problem is - How the function will know which table to query ? Do I need to pass table name as an argument to the Median function ? Do I need to pass column name as well ?
If so - it does not act as an aggregate function.
Is it possible at all ?

Thanks
Asi

Asi
Starting Member

3 Posts

Posted - 2009-04-26 : 15:49:52
Maybe I went into details too much... javascript:insertsmilie(''

The question is basically if it's possible to create an aggregate function in sql ?
If somebody has an example of any aggregate function that he wrote - I'll be happy to get it.

Thanks
Asi
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-04-26 : 16:41:16
You can write user-defined aggregate functions as CLR functions in SQL Server 2005 and 2008. Books Online has some examples, you can also find some here:

http://www.novicksoftware.com/Articles/sql-2005-product-user-defined-aggregate.htm
http://dotnetslackers.com/Community/blogs/basharkokash/archive/2008/06/07/how-to-implement-your-own-aggregate-function-in-sqlclr-sql-server-2005.aspx
Go to Top of Page

Asi
Starting Member

3 Posts

Posted - 2009-04-27 : 08:36:06
I have never written any CLR user defined function or any code in .NET Framework language, including Microsoft Visual Basic .NET and Microsoft Visual C#.

I have experience in T-SQL and VBA Programing.

Do you have a good reference to start with. I need to start with the working / programing environment, installations that I need etc...

The code itself and syntax will come along the way.

Thanks
Asi
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-04-27 : 08:50:17
Here's some books:

http://www.amazon.com/Professional-SQL-Server-2005-Programming/dp/0470054034/ref=sr_1_1?ie=UTF8&s=books&qid=1240836504&sr=8-1
http://www.amazon.com/Pro-SQL-Server-2005-Assemblies/dp/1590595661/ref=sr_1_4?ie=UTF8&s=books&qid=1240836504&sr=8-4

There's a number of examples online too. I Googled "SQL CLR median" and got some hits, but couldn't actually find the code for it.
Go to Top of Page

gabrielhuebsch
Starting Member

1 Post

Posted - 2010-01-20 : 09:23:46
I was just wondering you did ever get this median aggregate function working. I would love to see the code for this.
Thanks,
gabe
Go to Top of Page

netghoster
Starting Member

1 Post

Posted - 2011-04-01 : 17:41:46
quote:
Originally posted by Asi

I have never written any CLR user defined function or any code in .NET Framework language, including Microsoft Visual Basic .NET and Microsoft Visual C#.

I have experience in T-SQL and VBA Programing.

Do you have a good reference to start with. I need to start with the working / programing environment, installations that I need etc...

The code itself and syntax will come along the way.

Thanks
Asi




I just wonder have you ever got this done? I have exactly the same need to create this function to get me work done.
Go to Top of Page
   

- Advertisement -