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

转载于:https://www.cnblogs.com/jspdelphi/p/8472404.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值