This subsection explains a query that performs data calculations, using an example that converts temperature data that was collected in degrees Celsius to degrees Fahrenheit. To convert Celsius to Fahrenheit, the query needs to include a mathematical formula. The following figure shows the input and output data present when this query is executed.
Figure 2-17 Input and output data present when a query designed to perform data calculations is executed
![[Figure]](figure/zd021600.gif)
- Code
- To convert temperature from Celsius to Fahrenheit, you use arithmetic operations in the SELECT clause to express a mathematical formula.
REGISTER STREAM temperature_stream
(observation_time TIME, id INTEGER, temperature INTEGER);
REGISTER QUERY unit_conversion
ISTREAM (
SELECT observation_time, id,
temperature*9/5+32 AS fahrenheit_temperature
FROM temperature_stream[ROWS 1]); |
- Explanation
- The processing target of this query is the single most recent query. 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 temperature data expressed in Fahrenheit. In the SELECT clause, the code fragment temperature*9/5+32 is the arithmetic expression that converts from Celsius to Fahrenheit.
- To output calculated data, a name must be assigned to the calculation results. The keyword AS is used to specify the name of the resulting data. In the CQL code, fahrenheit_temperature is specified as the name for the data that results from the conversion to Fahrenheit.