Custom aggregate functions for SQL Server