Hi,

Maybe i show my concept not so clearly. 
Look how simple it is with my proposition and also how simple to understand by 
users.
Adriano, below you can see your samples addapted to my concept.

Function is called also for empty resultset once then agg_i=0.
Agg_i will control the whole execution choices – for every row it is increased 
by 1.
Vars declaration section is called onece at start of groupung level.

this can be simple for use also for udr
you can have there udr init for every groupin level – there you declare 
variables
and udr function (body) will be called for every row in grouping level


-- Works as standard SUM. 
create aggregate function custom_sum (i integer) returns (o integer)
as
begin
  if agg_i=1 then
    o = 0;
        
  if agg_i>0 then
    o = o + i;
end


-- Works as standard AVG.
create aggregate function custom_avg (i double precision) returns (o
double precision)
as
    declare accumulated double precision = 0;
begin
    if (i is not null) then
       accumulated = accumulated + i;   
        
  if (agg_finished and (agg_i>0)) then
    o = accumulated / agg_i;
end


-- Works as standard COUNT.
 create aggregate function custom_count (i integer) returns (o integer)
as
begin
  if (agg_finished) then
    o = agg_i;
end


-- This function shows the difference of returning value when
data set is not empty and returning in function termination when data
set is empty.
-- select custom_count_plus_1000(1) from rdb$database -- returns 1
-- select custom_count_plus_1000(1) from rdb$database where 1 = 0 --
returns 1000

create aggregate function custom_count_plus_1000 (i integer) returns (o
integer)
as
begin
  if agg_finished then
    Begin
       if agg_i>0 then
          o = agg_i; else
          o = 1000;
    end;
end



regards,
Karol Bieniaszewski
Firebird-Devel mailing list, web interface at 
https://lists.sourceforge.net/lists/listinfo/firebird-devel

Reply via email to