Calculated and Aggregated Fields (FireDAC)
Go Up to Working with DataSets (FireDAC)
FireDAC provides some types of calculated fields.
Contents
General
Calculated fields are virtual fields whose values are not fetched / stored in the database. Instead, they are calculated on the client side. FireDAC supports calculated fields of all TField.FieldKind types:
- fkCalculated--a simple calculated field. The value is calculated in the TDataSet.OnCalcFields event handler.
- fkInternalCalc--an advanced calculated field. The value can be assigned as to regular fields and is stored in the dataset records cache. It is calculated in the TDataSet.OnCalcFields event handler or using expressions specified by TField.DefaultExpression.
- fkLookup--a lookup field. The value is calculated automatically, providing a value from a lookup dataset for a key value in this dataset.
- fkAggregate--an aggregate calculating field. The value is calculated using an expression specified by TAggregateField.Expression, which includes the COUNT, SUM, MIN, MAX, AVG aggregate functions.
Only the fkInternalCalc and fkAggregate fields can be used in filtering, sorting, or locating operations. Also, they are stored with other dataset fields into persistent streams or files. The calculated field values cannot be posted to a database in automatic mode.
Note that TFDTable in live data window mode does not support aggregated fields.
Standard Calculated Fields
The fkCalculated and fkInternalCalc calculated field values may be assigned by the TDataSet.OnCalcFields event handler. A calculated field can be defined:
- At design time, using the dataset Fields Editor ... menu item.
- At run time, using the code. For example, to create a calculated field that contains upper-cased name, use the following:
procedure TForm1.Form1CalcFields(ADataSet: TDataSet);
begin
ADataSet.FieldByName('UName').AsString := UpperCase(ADataSet.FieldByName('Name').AsString);
end;
var
oField: TField;
i: Integer;
...
FDQuery1.FieldDefs.Updated := False;
FDQuery1.FieldDefs.Update;
for i := 0 to ADQuery1.FieldDefs.Count - 1 do
FDQuery1.FieldDefs[i].CreateField(Self);
oField := TStringField.Create(FDQuery1);
oField.Size := 50;
oField.FieldName := 'UName';
oField.FieldKind := fkInternalCalc; // or fkCalculated
oField.DataSet := FDQuery1;
FDQuery1.OnCalcFields := Form1CalcFields;
FDQuery1.Open;
Expression Calculated Fields
The fiInternalCalc fields can be calculated automatically by an expression specified by the TField.DefaultExpression. The TDataSet.OnCalcFields event handler and explicit value assignment are not needed then. The expression cannot be changed when the dataset is active. For example:
var
oField: TField;
i: Integer;
...
FDQuery1.FieldDefs.Updated := False;
FDQuery1.FieldDefs.Update;
for i := 0 to FDQuery1.FieldDefs.Count - 1 do
FDQuery1.FieldDefs[i].CreateField(Self);
oField := TStringField.Create(FDQuery1);
oField.Size := 50;
oField.FieldName := 'UName';
oField.FieldKind := fkInternalCalc;
oField.DefaultExpression := 'UPPER(Name)';
oField.DataSet := FDQuery1;
FDQuery1.Open;
Aggregated Fields
The fkAggregate aggregated fields management is similar to the expression calculated fields management. FireDAC calculates aggregated fields when TFDDataSet.AggregatesActive is set to True (by default, its value is set to False). The aggregated expression cannot be changed when the dataset is active. For example, to create an aggregated field, proceed as following:
var
oField: TAggregateField;
i: Integer;
...
FDQuery1.FieldDefs.Updated := False;
FDQuery1.FieldDefs.Update;
for i := 0 to FDQuery1.FieldDefs.Count - 1 do
FDQuery1.FieldDefs[i].CreateField(Self);
oField := TAggregateField.Create(FDQuery1);
oField.FieldName := 'Total';
oField.Expression := 'SUM((ItemPrice + ItemTaxes) * ItemCount)';
oField.DataSet := FDQuery1;
FDQuery1.AggregatesActive := True;
FDQuery1.Open;
An aggregated field may define grouping. Then a value is calculated for records with the same index field values, instead of all records. To define the grouping, perform the following steps:
- Set TAggregateField.IndexName to the name of the index to use for grouping. By default, the current index is used.
- Set TAggregateField.GroupingLevel to a number of indexed fields to use for grouping. By default, its value is set to 0 (no fields and no grouping).
Note that the aggregated field always return Null when a dataset is in dsInsert state.
Aggregated Values
Also, the FireDAC application can use the TFDDataSet.Aggregates collection to define aggregated values. They are more light-weighted than fields and can be defined at any time, including when the dataset is active. For example:
with FDQuery1.Aggregates.Add do begin
Name := 'Total';
Expression := 'SUM((ItemPrice + ItemTaxes) * ItemCount)';
Active := True;
end;
FDQuery1.AggregatesActive := True;
...
Label1.Caption := VarToStr(FDQuery1.Aggregates[0].Value);
See Also
Samples
- FireDAC TFDMemTable Main sample
- FireDAC TFDQuery Aggregates sample