AnyDAC
ContentsIndexHome
PreviousUpNext
TADQuery.Aggregates Property

The collection of client side aggregates, defined for dataset.

Group
Links

Use Aggregates to define client side aggregating formulas, that will be automatically maintained and calculated for group of or for all records of dataset. 

All records in the group of records has the same field values for defined set of fields. Aggregates performing calculation for group of records must be associated with one of the indexes. These aggregate expressions will be calculated only is associated index is current and active. 

Adding aggregate field does not add TADAggregate object to Aggregates collection. That are two alternative ways - to use aggregate fields or to use Aggregates. 

DataSet will automatically maintain and calculate Aggregates values when dataset is fetching the data or application edits the data, if AggregatesActive is True. If application needs to perform large updates to dataset and aggregate values are not needed while updating, then set AggregatesActive to False before updates, and return to original value after updates. Also see BeginBatch / EndBatch methods. 

When aggregates are maintained, the Value method of every active aggregate object returns a value that reflects the current data in the dataset. When users edit the data in the dataset, these values are recalculated to reflect the user's changes. 

The expressions in Aggregates must contain aggregating functions, like a SUM, COUNT. AnyDAC supports expression syntax compatible with:

  • BDE data access components;
  • TClientDataset;
  • Oracle 8 (not for 100%).

See Writing Expressions for details of how to write constraint expressions.

property Aggregates: TADAggregates;

Calculated and Aggregated Fields, AggregatesActive, BeginBatch, EndBatch

with ADMemTable1.Aggregates.Add do begin
  Expression := 'sum(sal + bonus)';
  Active := True;
end;
with ADMemTable1.Aggregates.Add do begin
  Expression := 'sum(sal + bonus)';
  IndexName := 'by_deps';
  Active := True;
end;
ADMemTable1.IndexName := 'by_deps';
ADMemTable1.AggregatesActive := True;

Label1.Caption := 'Total payments : ' + VarToStr(ADMemTable1.Aggregates[0].Value);
Label2.Caption := 'Current department payments : ' + VarToStr(ADMemTable1.Aggregates[1].Value);

See AnyDAC\Samples\Comp Layer\TADQuery\Aggregates sample for details.

What do you think about this topic? Send feedback!