• Virtuoso coding, certainly, but why not take advantage of SQL Server's ability to create custom aggregation functions? Microsoft's example can be found here:

    http://msdn.microsoft.com/en-us/library/ms182741.aspx