Subject | Re: [firebird-support] Re: SQL Question Count() statistics |
---|---|
Author | Marco Bommeljé |
Post date | 2003-10-17T13:10:21Z |
tanz_anthrox wrote:
To produce the result set that you want, it should look something like:
CREATE PROCEDURE COUNTNAMES
RETURNS (
Name VARCHAR(40),
TrueCount INTEGER,
FalseCount INTEGER)
AS
DECLARE VARIABLE name
BEGIN
FOR SELECT Name FROM table INTO :Name
DO
BEGIN
SELECT count(*) FROM table t
WHERE t.Name= :name AND t2."True/False"="T"
INTO :TrueCount;
SELECT count(*) FROM table t
WHERE t.Name= :name AND t2."True/False"="F"
INTO :FalseCount;
SUSPEND; /* Returns result then resumes at end of block */
END
END
Then, from the client application, you can use the SP to select
from as if it were a table:
SELECT Name, TrueCount, FalseCount FROM CountNames
In addition, you might want to dopwnload the manuals on
Interbase/Firebird's Procedural SQL language.
Good Luck,
Marco
> I tried to make StoredProcedure likeJudging from the source code, this procedure returns a single row.
>
> CREATE PROCEDURE COUNTNAMES
> RETURNS (
> TRUTH INTEGER,
> FAITH INTEGER)
> AS
> BEGIN
> SELECT Name,
> (SELECT count(*) FROM table t2
> WHERE t1.Name=t2.Name and t2."True/False"="T")
> as "True count",
> (SELECT count(*) FROM table t2
> WHERE t1.Name=t2.Name and t2."True/False"="F")
> as "False count"
> FROM table t1
> GROUP by Name
> INTO :TRUTH,:FAITH;
> SUSPEND; /* I DONT KNOW WHY !!*/
> END
>
> But this procedure returns a RESULT SET not a ROW;
To produce the result set that you want, it should look something like:
CREATE PROCEDURE COUNTNAMES
RETURNS (
Name VARCHAR(40),
TrueCount INTEGER,
FalseCount INTEGER)
AS
DECLARE VARIABLE name
BEGIN
FOR SELECT Name FROM table INTO :Name
DO
BEGIN
SELECT count(*) FROM table t
WHERE t.Name= :name AND t2."True/False"="T"
INTO :TrueCount;
SELECT count(*) FROM table t
WHERE t.Name= :name AND t2."True/False"="F"
INTO :FalseCount;
SUSPEND; /* Returns result then resumes at end of block */
END
END
Then, from the client application, you can use the SP to select
from as if it were a table:
SELECT Name, TrueCount, FalseCount FROM CountNames
In addition, you might want to dopwnload the manuals on
Interbase/Firebird's Procedural SQL language.
Good Luck,
Marco