2023年2月8日 星期三

Delphi TAggregate Row Summary

Ex:

var 
  sFieldName :String;
  fdAggregate:TAggregate;

begin
  cdsTemp := TClientDataset.Create(Self);
  cdsTemp.Aggregates.Clear;
  cdsTemp.AggregatesActive := True;
  cdsTemp.CloneCursor(Master, False, True);

  for i:=0 to Master.FieldCount-1 do
  begin
    sFieldName := Master.Fields[i].FieldName;
    if cdsTemp.FieldByName(sFieldName).DataType in 
      [ftSmallint, ftInteger, ftFloat, ftCurrency, ftLargeint] then
    begin
      fdAggregate := TAggregate.Create(cdsTemp.Aggregates, cdsTemp);
      fdAggregate.AggregateName := 'agg' + sFieldName;
      fdAggregate.Expression := 'sum('+sFieldName+')';
      fdAggregate.Active := True;
    end;
  end;

  //Get Value
  cdsTemp.Aggregates.Find('agg'+sFieldName).Value;
end;