Subject Re: [firebird-support] General SQL question. What is the best way?
Author Adomas Urbanavicius
For long time running linear queries i use stored proc with loop to
return sum values. By such SP you can even get sums for groups
something like
for select room,cost ... order by room do
if room <> tagged_room
..... Do Your Finalization of action Here
cost_sum = cost_sum;
tagged_room = room;
else
cost_sum = cost_sum + cost;

But this is extreme optimization with data overhead, which I use only in
some cases, where simple queries return after 100-300secs, so sums also
would be after
100 - 300sec, and total 200-600secs.



Anyway the best way to get results is to use agregate grouping with
indexed fields.

>Hi,
>
>I have a long running query that returns some patient names with the
>tests and the prices like this.
>
>PATIENT1, ROOM#, TEST NAME, COST
>PATIENT2, ROOM#, TEST NAME, COST
>PATIENT3, ROOM#, TEST NAME, COST
>..
>
>
>This is ok, but I also want to get the SUM of the COST FIELD. How can
>I do it? Run the same long running query again with the "SUM"
>function? Or is it better looping through the query results while
>summing up the costs field? Or is it better using "group by" ?
>
>Any ideas? Thank you.
>
>
>