2.3.2 Retrieving data that satisfies a condition
(1) Query that outputs data of temperature 0 oC or lower
This subsection explains a query designed to output data that satisfies a single condition: output data of temperature 0
C or lower. The following figure shows the input and output data present when this query is executed.
Figure 2-15 Input and output data present when a query that satisfies a single condition is executed
![[Figure]](figure/zd021400.gif)
- Code
- To output data that satisfies the condition of temperature 0
C or lower, 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 below_zero_filter
ISTREAM (
SELECT observation_time, id, temperature
FROM temperature_stream[ROWS 1]
WHERE temperature <= 0); |
- Explanation
- The processing target of this query is the single most recent tuple to meet the condition. The FROM clause specifies a ROWS window, which retrieves tuples from the stream data in terms of the number of tuples.
- The output data is data of temperature 0
C or lower. In the WHERE clause, temperature <= 0 is specified. This specification causes the clause to compare the value of temperature to 0, and selects tuples in which the value of temperature is less than or equal to 0.
(2) Query that outputs data of temperature between -10 oC and 0 oC (inclusive)
This subsection explains a query designed to output data that satisfies two conditions: output data of temperature between -10
C or higher and 0
C or lower (inclusive). The following figure shows the input and output data present when this query is executed.
Figure 2-16 Input and output data present when a query designed to output data that satisfies two conditions is executed
![[Figure]](figure/zd021500.gif)
- Code
- To output data that satisfies the conditions of temperatures -10
C or higher and 0
C or lower (inclusive), you use comparison operators and the logical operator AND in the WHERE clause to specify a data selection condition. REGISTER STREAM temperature_stream
(observation_time TIME, id INTEGER, temperature INTEGER);
REGISTER QUERY temperature_range_filter
ISTREAM (
SELECT observation_time, id, temperature
FROM temperature_stream[ROWS 1]
WHERE -10 <= temperature AND temperature <= 0); |
- Explanation
- The processing target of this query is the single most recent tuple to meet the condition. The FROM clause specifies a ROWS window, which retrieves tuples from the stream data in terms of the number of tuples.
- The output data is data of temperature -10
C or higher and 0
C or lower. In the WHERE clause, -10 <= temperature and temperature <= 0 are specified as linked conditions. This specification causes the clause to compare the value of temperature to -10, selecting tuples in which the value of temperature is greater than or equal to -10, AND it compares the value of temperature to 0, selecting tuples in which the value of temperature is less than or equal to 0.