Subject Re: To calculate...
Author Adam
--- In firebird-support@yahoogroups.com, Marcelo Machado
<machado6655@y...> wrote:
>
> Dear friends,
>
> To calculate 27 fields I do need " to write " all (see example) or
does some exist formula that calculates of " such a " until " such "?
>
> ALTER TABLE new table
> ADD TOTAL COMPUTED BY (V1+V2+V3+V4... )
>
> OR
>
> ALTER TABLE new table
> ADD TOTAL COMPUTED BY (V1 ? V27)
>

No, field orders can be changed too using alter statement, so even if
there was it would be unsafe to rely on.

CREATE TABLE TBL1
(
ID INTEGER,
BLAH VARCHAR(20)
);

CREATE TABLE TBL1DATA
(
ID INTEGER,
TBL1ID INTEGER,
VALUE INTEGER
);

You can then create a join and group by, and use a view to get the
total column rather than the computed field (which is very slow for
cross table joins).

select t1.ID, tl.blah, sum(t2.value) as Total
from TBL1 t1
join TBL1DATA d on (t1.id = d.tbl1id)
group by t1.id, t1.blah

You can then add or remove V1 etc without making DDL changes and
hitting your 255 table change limit from your other thread.

Adam