I used calculated fields in Delphi, but only to calculate values from diferent columns of the same row. I do not think that calculated fields are intented to do calculations of more rows.
For example, if you have fields Amount1 and Amount2 in a table, you could define calculated field AverageAmount and in query's OnCalcFields event put something like this:
Query1.FieldByName('AverageAmount').AsFloat := 0.5 * (Query1.FieldByName('Amount1').AsFloat + Query1.FieldByName('Amount2').AsFloat);
However, I would not use calculated fields for calculating sum of one column in a whole table (and, if I understood you well, that is what you want to do), but I would do some coding:
var
SumField1: Integer;
...
SumField1 := 0;
Query1.DisableControls;
try
Query1.First;
while not Query1.EOF do begin
SumField1 := SumField1 + Query1.FieldByName(Field1).AsInteger;
Query1.Next;
end;
finally
Query1.EnableControls;
end;
Or, you could define a separate query which gives you sum:
var
SumField1: Integer;
Q: TZReadOnlyQuery; //-- that's if you use Zeos,
{ or Q: TSQLQuery if you use SQLdb components.}
....
Q := TZReadOnlyQuery.Create(Self); // or TSQLQuery.Create(Self);
try
Q.Connection := ZConnection1; // or Q.Database := ...
Q.SQL.Clear;
Q.SQL.Append('select sum(Field1) as SumF1 from Table1');
Q.Open;
if not Q.IsEmpty then begin
Q.First;
SumField1 := Q.FieldByName(SumF1).AsInteger;
end else
SumField1 := 0;
finally
Q.Free;
end;