User-Defined Aggregates on 2 Columns

I'm wondering if it possible to create a user-defined aggregate that operates on two columns instead of one. Basically, I want to do something like a weighted average where I multiply two columns in each row, sum up the results, and then divide by the sum of one of the fields.

For example, if I had a table that had two columns, call them SuccessRate and TrialSize, and I want to find the WeightedAverageSuccessRate, assuming that the larger the trial size of each data-point, the heavier its success rate would weigh. Hopefully I'm making sense here.

I'd love to just be able to do something like:

SELECT WeightedAVE(SuccessRate, TrialSize) 'WeightedAverageSuccessRate'
FROM MyTable

Although I haven't tried it yet, I'm assuming that this won't work...?

If you have any thoughts, please let me know.

Thanks,
Chris

[846 byte] By [ChrisWiederspan] at [2008-2-28]
# 1
Hi Chris,

Unfortunately, we don't have mutli-valued aggregates in SQL Server 2005. It's definately something we're looking at. You may be able to work around this by wrapping the values in a UDT and passing that into the aggregate.

Cheers,
-Isaac

IsaacKunen at 2007-9-9 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...

SQL Server

Site Classified