FireDAC.TFDQuery.Aggregates Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample demonstrates how to define the client-side aggregating formulas with TFDQuery.

Location

You can find the Aggregates sample project at:

  • Start | Programs | Embarcadero RAD Studio Alexandria | Samples and then navigate to:
    • Object Pascal\Database\FireDAC\Samples\Comp Layer\TFDQuery\Aggregates
  • Subversion Repository:
    • You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

Description

The Aggregates sample shows you how to use the Aggregates property of TFDQuery to define the client-side aggregating formulas. To this end, the sample implements different preset aggregate expressions that contains the following aggregating functions: AVG , SUM and MAX. Moreover, you can edit your own aggregate expressions and apply them to the records of a dataset in order to evaluate the expression.

How to Use the Sample

  1. Navigate to the location given above and open Aggregates.dproj.
  2. Press F9 or choose Run > Run.

Files

File in Delphi Contains

Aggregates.dproj
Aggregates.dpr

The project itself.

fAggregates.pas
fAggregates.fmx

The main form.

Implementation

Before running the sample, the main components are configured at design time using the Object Inspector as follows:

  • A TFDQuery object named qryAggregates. This component is used to implement a dataset capable of executing SQL queries. The following setup is needed:
  • Configure the Connection property to specify the FireDAC connection object that is used to connect to a DBMS.
  • Set the SQL property to select * from {id Orders}. It specifies the SQL statement to execute for the query.
  • Edit the Aggregates property with 4 aggregate expressions:
    1. AVG(Freight)
    2. SUM(Freight)
    3. MAX(OrderID)
    4. Typed by the user using the edit box.
  • A TDataSource object named DataSource1. This component provides an interface between a dataset component and data-aware controls on a form. In this sample, it is used to provide communication between the dataset and the grid where the dataset is displayed. To this end, the DataSet property of DataSource1 is set to qryAggregates and the DataSource property of DBGrid1 is set to DataSource1.

When you run the application, click on the Use Connection Definition combo box and select an option in order to define a connection. When you select an item of the combo box, the sample uses the Open method of qryAggregates in order to execute the SQL statement that retrieves the "Orders" table from the database. Then, the sample displays the "Orders" table using a TDBGrid component. Once the DBGrid1 is filled, you can interact with the sample in the following ways:

  1. Set an aggregate expression. Type your own aggregate expression on the Try edit box and press Enter for evaluation. For example: type MAX(Freight) and press Enter to see the maximum value of the Freight field on the Result edit box.
  2. Preset aggregate expressions. See the evaluation of the preset aggregate expressions.

Uses

See Also

Samples