THe following is a structure for user defined aggregate function.
The MaxByteSize must be set to -1 as it's userd defeined format
The Accumulate function defines how to handle new data rows coming in.
THe Merge function defines how to merge two aggregates
The Terminate functions finally returns the aggregated value
As its user defined, it needs to implement IBinarySerialize and define the read/write methods.
The read write methods use binary reader writer to serialize the local data,
and reconstruct the local data from binary stream.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections.Generic;
using System.Data.Sql;
using System.IO;
[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate(Format.UserDefined,
MaxByteSize = -1,
IsInvariantToNulls = true,
IsInvariantToDuplicates = false,
IsInvariantToOrder = true,
IsNullIfEmpty = true)]
public struct AWP_LAST_VALUE : IBinarySerialize
{
private SortedList<string, value> data;
public void Init()
{
data = new SortedList<string, value>();
}
public void Accumulate([SqlFacet(MaxSize = 100)] SqlString ID)
{
...
}
public void Merge(AWP_LAST_VALUE alv)
{
...
}
[return: SqlFacet(MaxSize = 100)]
public SqlString Terminate()
{
...
return null;
}
public void Read(BinaryReader r)
{
}
public void Write(BinaryWriter w)
{
}
}