Analytical Functions
# StreamSQL Analytical Functions
Analytical functions are used for complex analytical calculations in data streams, supporting state management and historical data access.
# LAG - Lag Function
Syntax: lag(col, offset, default_value)
Description: Returns the value from the Nth row before the current row. offset specifies the offset amount, default_value is the default value.
Incremental Calculation: ✅ Supported
Example:
SELECT device, temperature, lag(temperature, 1) as prev_temp
FROM stream
GROUP BY device, TumblingWindow('10s')
2
3
# LATEST - Latest Value Function
Syntax: latest(col)
Description: Returns the latest value for the specified column.
Incremental Calculation: ✅ Supported
Example:
SELECT device, latest(temperature) as current_temp
FROM stream
GROUP BY device, TumblingWindow('10s')
2
3
# CHANGED_COL - Changed Column Function
Syntax: changed_col(row_data)
Description: Returns an array of column names that have changed.
Incremental Calculation: ✅ Supported
Example:
SELECT device, changed_col(*) as changed_columns
FROM stream
GROUP BY device, TumblingWindow('10s')
2
3
# HAD_CHANGED - Change Detection Function
Syntax: had_changed(col)
Description: Determines whether the value of the specified column has changed, returning a boolean value.
Incremental Calculation: ✅ Supported
Example:
SELECT device, had_changed(status) as status_changed
FROM stream
GROUP BY device, TumblingWindow('10s')
2
3
# 📚 Related Documentation
- Aggregate Functions - Learn detailed usage of aggregate functions
- Window Functions - Learn detailed usage of window functions
- SQL Reference - View complete SQL syntax reference