Example: AVG over a user-defined type (UDT)
This example implements the AVG aggregate function over the CANADIAN_DOLLAR distinct type.
Strong typing prevents you from using the built-in AVG function on a distinct type. It turns out that the source type for CANADIAN_DOLLAR was DECIMAL, and so you implement the AVG by sourcing it on the AVG(DECIMAL) built-in function.
CREATE FUNCTION AVG (CANADIAN_DOLLAR)
RETURNS CANADIAN_DOLLAR
SOURCE "QSYS2".AVG(DECIMAL(9,2))Note that in the SOURCE clause you have qualified the function name, just in case there might be some other AVG function lurking in your SQL path.