Subject Re: [firebird-support] PSQL: Getting both aggregation and individual rows
Author Mark Rotteveel
On Tue, 21 Oct 2014 09:39:44 +0200, "Josef KokeŇ° j.kokes@...
[firebird-support]" <> wrote:
> Hi!
> Is there a simple way to get an aggregation of some query as well as the
> individual rows from within a PSQL? I.e., I have a stored procedure
> GEN_DATA which produces individual rows (ID, Name, DateAndTime, Value).
> I am processing the output of GEN_DATA in another stored procedure
> FOR SELECT id, name, dateandtime, value
> FROM gen_data(...)
> INTO ...
> DO ...
> Within this loop, I need the individual records, but I also need some
> aggregations on them, i.e. MIN(dateandtime) or SUM(value). If I could
> run GEN_DATA twice, then it would be easy (in the first run I would
> calculate the aggregations, in the second run the individual values),
> but I can't. If I could perform the processing in my application (rather
> than the stored procedure), it would be easy, too (I would simply
> traverse the result dataset of GEN_DATA twice), but again I can't do
> that. Would Common Table Expressions or perhaps Derived Tables help me?

Not really an answer right now, but with Firebird 3 you would be able to
apply the window functions to obtain aggregate values inside the individual
rows, although I am unsure if this plays nice with the record stream of a
selectable stored procedure.