Calculated and Aggregated Fields,Add Calculated field at runtime

Posted 后凤凰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Calculated and Aggregated Fields,Add Calculated field at runtime相关的知识,希望对你有一定的参考价值。

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Calculated_and_Aggregated_Fields_(FireDAC)

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:

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);

以上是关于Calculated and Aggregated Fields,Add Calculated field at runtime的主要内容,如果未能解决你的问题,请参考以下文章

[HDOJ5439]Aggregated Counting(乱搞)

Aggregated Counting(找规律 + 预处理)

OpenShift Aggregated Logging:解析 Apache 访问日志

powershell use calculated properties

如何将GROUP BY与AGGREGATED列值一起使用

mysql:In aggregated query without GROUP BY, expression #1 of SELECT list contains...........