This subsection explains a query that categorizes multiple data items and then summarizes the data, using an example that outputs one-minute average temperatures for each observation site. The following figure shows the input and output data present when this query is executed.
Figure 2-19 Input and output data present when a query designed to categorize and summarize data is executed
![[Figure]](figure/zd021800.gif)
- Code
- To categorize data, you use the GROUP BY clause to specify how the data is to be categorized. You specify the GROUP BY clause immediately after the WHERE clause (or immediately following the FROM clause if the WHERE clause is not specified).
REGISTER STREAM temperature_stream
(observation_time TIME, id INTEGER, temperature INTEGER);
REGISTER QUERY average_calculation
ISTREAM (
SELECT id, AVG(temperature) AS average_temperature
FROM temperature_stream[RANGE 1 MINUTE]
GROUP BY id); |
- 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 from each observation site. 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 GROUP BY clause, id is specified to group data by observation site.
- When data processing is specified in both the SELECT clause and the GROUP BY clause, the SELECT clause is executed according to the category defined in the GROUP BY clause.