News, views, tips and tricks on Oracle and other fun stuff

User-Defined Aggregate Functions

I had a requirement to write a query that should return a concatenation of column values from different rows and grouped by another column. Well, that would be easy if Oracle provided an aggregate function similar to SUM or COUNT for example, but instead of summing or counting, it would concatenate. Alas! There is no such pre-defined function in Oracle. But, don’t despair. Oracle has something called user-defined aggregate functions, meaning that you can define your own aggregate function, or even change the behavior of a pre-defined one such as MAX, MIN, SUM.

User-defined aggregate functions are used in SQL DML statements just like Oracle’s own built-in aggregates. Once such functions are registered with the server, Oracle simply invokes the aggregation routines that you supplied instead of the native ones.

User-defined aggregates can be used with scalar data too. For example, it may be worthwhile to implement special aggregate functions for working with complex statistical data associated with financial or scientific applications.

User-defined aggregates are a feature of the Extensibility Framework. You implement them using ODCIAggregate interface routines.

For more information see chapter 11: User-Defined Aggregate Functions in Oracle9i Data Cartridge Developer’s Guide.

Filed in Oracle on 05 Jul 05 | Tags: , ,

Reader's Comments

  1. |

    That sounds very intersting – but how can you change the behaviour of a SUM or a COUNT. For example:

    SELECT COUNT(*) from mytable

    should use my COUNT function not the real one

    Thanks Manfred

  2. |

    Instead of changing the behavior of a built-in function, I would create my own user-defined aggregate function and use it in my queries like this:

    select myCount(*) from myTable;

  3. |

    Hello Eddie,

    that works fine. But in my case I need to overwrite the COUNT, MIN, MAX aggregation functions.

    Do you know, how this can be done ?

    Thank you for your help. Manfred