2.6.2 Definition examples in which time division is specified

This subsection gives examples of the following two types of query definitions in which time division is specified:

In this example, a relation operation using an aggregate function is performed on the arriving tuples that are in the 1-minute RANGE window. The following stream is handled in this example:

Stream handled in the time division specification example
  • The stream name is stock.
  • Contains a column named price.
Organization of this subsection
(1) Example in which a single aggregate function is defined
(2) Example in which multiple aggregate functions are defined

(1) Example in which a single aggregate function is defined

In the following definition example, the sum total of the column price in the stream stock is determined from the tuples that are in the RANGE window.

REGISTER STREAM stock(price INTEGER, name VARCHAR(10)); ...(1)
REGISTER QUERY_ATTRIBUTE q1 STREAM_NAME=stock PERIOD=300MS TARGETS=SUM(price);    ...(2)
REGISTER QUERY q1      ...(3)
ISTREAM(
 SELECT name, SUM (price) AS s1
 FROM stock[RANGE 1 MINUTE]
 GROUP BY name
);

Note:
Numbers (1)-(3) correspond to the explanations shown below. They are not coded in the actual definition.

The name of the query in which time division is used is ql specified in (3). Therefore, the query name ql is also specified in the QUERY_ATTRIBUTE statement in (2).

In this example, the sum total of the column price is determined using time division for the stream stock specified in (1). Therefore, stock is specified for the STREAM_NAME= data identifier in (2) and the aggregate function SUM (price) is specified for TARGETS=.

If you want to determine the maximum or minimum value instead of the sum total, specify the aggregate function MAX or MIN.

(2) Example in which multiple aggregate functions are defined

In the following definition example, the sum total, maximum value, and minimum value of the column price in the stream stock are determined from the tuples in the RANGE window.

REGISTER STREAM stock(price INTEGER, name VARCHAR(10));
REGISTER QUERY q0
ISTREAM(
 SELECT name, price AS price0, price AS price1, price AS price2
 FROM stock[NOW]
);
REGISTER QUERY_ATTRIBUTE q1 STREAM_NAME=q0 PERIOD=300MS
TARGETS=SUM(price0),MAX(price1),MIN(price2);
REGISTER QUERY q1
ISTREAM(
 SELECT q0.name, SUM(q0.price0) AS sum_price,
   MAX(q0.price1) AS max_price, MIN(q0.price2) AS min_price
 FROM q0[RANGE 1 MINUTE]
GROUP BY q0.name
);

When using time division, you cannot execute multiple aggregate functions for the same column name of the same data identifier at the same time. For example, you cannot specify TARGETS=SUM(price),MAX(price),MIN(price) for a query on column name price of the data identifier stock.

When specifying multiple aggregate functions, you must provide separate column names as the targets of the aggregate functions. In this example, the individual aggregate functions SUM, MAX, and MIN are executed after the column name is changed from price to price0, price1, and price2, respectively.