Sometimes you need to perform some complex SQL queries depending on some aggregate operations or functions. In most cases, these complex operations could be achieved by series of queries with joins and groupings, but, sometimes it is almost impossible to achieve this by the basic queries and functions natively supported by SQL. In this case, you need to create your SQL custom user-defined aggregate functions.
This article is a walkthrough which takes you from the early steps of writing the code up to its final form.