Subject Re: [firebird-support] Re: Aggregate UDF?
Author Michael Ludwig
Svein Erling schrieb am 25.06.2010 um 11:16 (-0000):

> [...] isn't it an aggregate function that requires 'going through'
> records twice (first finding the average and then how much each value
> deviates from this value)?

Yes, exactly. Conceptually, it cannot be done in one pass.

> CREATE PROCEDURE P_STDDEV (
> FIELD VarChar(32),
> FROMCLAUSE VarChar(1900))
> returns (
> STDDEV Double Precision)

Great!

> S = 'SELECT COUNT('||FIELD||') FROM '|| FROMCLAUSE;
> EXECUTE STATEMENT S INTO :NORECORDS;
> S = 'SELECT AVG('||FIELD||') FROM '|| FROMCLAUSE;
> EXECUTE STATEMENT S INTO :MYAVERAGE;

You could combine these two into one statement, it would probably be
more efficient:

S = 'SELECT COUNT('||FIELD||'), AVG('||FIELD||') FROM '|| FROMCLAUSE;
EXECUTE STATEMENT S INTO :NORECORDS, :MYAVERAGE;

--
Michael Ludwig