Subject Re: [firebird-support] Re: SQL Question Count() statistics
Author Dimitry Sibiryakov
On 17 Oct 2003 at 12:43, tanz_anthrox wrote:

>You wrote : "SP is one of them" You mean storedProcedure ??

Yes.

>But this procedure returns a RESULT SET not a ROW;

Do you need only one row? For particular name?

>How Can I implement this by using Stored Procedure.

You should use FOR SELECT ... DO clause and may put nested selects
into loop body. That's all difference. Of course, you also can do
cycle though raw results and increment counters. This even may be
faster...

Something like this:

NAME = null;
TRUTH = 0;
FAITH = 0;
FOR SELECT NAME, RESULT FROM TABLE ORDER BY NAME
INTO :NEW_NAME, :RESULT DO
BEGIN
IF (NAME=NEW_NAME) THEN
BEGIN
IF (RESULT='T') THEN
TRUTH = TRUTH+1;
ELSE
FAITH = FAITH+1;
END
ELSE
BEGIN
IF (NAME IS NOT NULL) THEN
SUSPEND;
TRUTH = 0;
FAITH = 0;
NAME = NEW_NAME;
END;
END;
IF (NAME IS NOT NULL) THEN
SUSPEND; // to return the last result

SY, Dimitry Sibiryakov.