zoukankan      html  css  js  c++  java
  • 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);
    

  • 相关阅读:
    MySQL字段数据全部查出【只保留中文、英文、数字、空格的词表】
    MySQL查看当前运行的事务和执行的账户
    【转】【MySQL报错】ERROR 1558 (HY000): Column count of mysql.user is wrong. Expected 43, found 39.
    【转】mysqldump的锁表的问题
    mysql 通过echo的方式写入数据库 中文乱码解决方案
    Python3.5爬取豆瓣电视剧数据并且同步到mysql中
    Python3.5爬取cbooo.cn数据并且同步到mysql中
    【转&参考】MySQL利用frm和ibd文件进行数据恢复
    [算法]从一道题引出variable-precision SWAR算法
    [转]nginx负载均衡的五种算法
  • 原文地址:https://www.cnblogs.com/jspdelphi/p/8472404.html
Copyright © 2011-2022 走看看