This subsection explains a query that summarizes data, using an example that outputs average temperatures in one-minute intervals from observation site 1 (ID: 1). First, the query summarizes one minute of temperature data from ID1, and then it derives an average from the summary results. The following figure shows the input and output data present when this query is executed.
Figure 2-18 Input and output data present when a query designed to summarize data is executed
![[Figure]](figure/zd021700.gif)
- Code
- To summarize data, you use an aggregate function in the SELECT clause to specify how you want the data to be summarized. To target specific data for summarization, you use a comparison operator in the WHERE clause to specify a data selection condition.
REGISTER STREAM temperature_stream
(observation_time TIME, id INTEGER, temperature INTEGER);
REGISTER QUERY average_calculation_id_1
ISTREAM (
SELECT AVG(temperature) AS average_temperature
FROM temperature_stream[RANGE 1 MINUTE]
WHERE id = 1); |
- Explanation
- The processing target of this query is all of the tuples from the last minute. The FROM clause specifies a RANGE window, which retrieves tuples from the stream data in terms of time.
- The output data is one-minute averages of the temperatures observed at site 1 (ID1). In the SELECT clause, the aggregate function AVG is specified to compute the average value of temperature, and average_temperature is specified as the name of the data item into which the results are output. In the WHERE clause, id = 1 is specified as the target of the computational processing.
- When data processing is specified in both the SELECT and the WHERE clauses, the WHERE clause is executed first, and then the SELECT clause is executed on the results from the WHERE clause.
- The tuple group selected by the WHERE clause, which is executed first, is called an intermediate relation. In this query, this is the tuple group from the last minute that is selected only from observation site ID1.