2.3.3 Example of processing using an aggregate function

This subsection shows an example of calculating the average rate of price increase for each stock, using the GROUP BY clause and the aggregate function AVG for tuples that contain a stock name and a rate of price increase.

An example of a CQL definition is described below. In the actual CQL definition, use single-byte alphanumeric characters to specify the column names. For AVG, specify an alias.

REGISTER QUERY q
  SELECT AVG(rate-of-price-increase)
  FROM s1[ROWS 3] GROUP BY stock-name;

The following figure shows the input and output relations when this CQL is executed.

Figure 2-12 Example of processing using an aggregate function

[Figure]

The horizontal axis indicates time, which advances from left to right. Times t1 to t6 indicate the time at which tuples arrived.

In this example, for each stock name identified using the GROUP BY clause, the aggregate function AVG (for calculating the average) is used to specify the rate of price increase. Note the input relation here has a duration specified by [ROWS 3].

The set in the input relation at time tA is (Stock A, 1.0)(Stock A, 0.8) and the average by stock name is (Stock A, 0.9).

Likewise, the set at time tB is (Stock A, 1.0)(Stock A, 1.2)(Stock B, 1.3), and the averages by stock name are (Stock A, 1.1)(Stock B, 1.3).

In this way, the average rate of price increase by stock name at the specified time is extracted to the output relation as tuples that depend on the lifespan of the tuples in the input relation.