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

  • 相关阅读:
    波场TRX 钱包开发,看这篇就够了
    比特币(BTC)钱包对接之如何实现平台用户注册地址生成?
    如何搭建泰达币(USDT)钱包节点?
    交易所如何对接狗狗币(DOGE)钱包?这点不可忽视
    【转】影响加密数字货币交易情绪有哪些?如何控制?
    探寻DOT充提币接口对接流程
    你需要PCIE×4硬盘
    安装rename perl version
    Emacs-log
    Biopython Numpy 安装问题
  • 原文地址:https://www.cnblogs.com/jspdelphi/p/8472404.html
Copyright © 2020-2023  润新知