nonNegativeDerivative
Computes the non-negative derivative of metric_column with respect to timestamp_column.
This is a ClickHouse-specific window function, not part of standard SQL.
For each row, the derivative is computed against the previous row in the window's evaluation order, which is determined by the window's ORDER BY clause - not by timestamp_column.
The timestamp_column argument is read only to measure the elapsed time between the current row and that previous row; it does not order the rows itself.
nonNegativeDerivative does not order rows by timestamp_column; the window's ORDER BY does.
For the formula below to apply, timestamp_column must be strictly increasing in the window's evaluation order, so you should normally order the window by timestamp_column ascending (for example ... OVER (ORDER BY ts ASC) together with nonNegativeDerivative(metric, ts)).
Whenever the elapsed time between the current row and the previous row is non-positive - which happens with ORDER BY timestamp_column DESC or with duplicate (equal) timestamps - the function returns 0 for that row instead of following the formula.
The result is the rate of change of the metric per INTERVAL, with any negative value clamped to 0.
This is useful for monotonically increasing metrics, such as counters, where a decrease usually indicates a reset rather than a real negative rate.
Syntax
For more detail on window function syntax see: Window Functions - Syntax.
Arguments
metric_column— The column whose derivative is computed. (U)Int* or Float*.timestamp_column— The column used to measure the elapsed time between the current row and the previous row in the window order. It does not order the rows; the window'sORDER BYdoes, and should normally use this same column. DateTime or DateTime64.INTERVAL X UNITS— Optional. The time unit the result is scaled to. Defaults toINTERVAL 1 SECOND. Only fixed-length units are supported (NANOSECOND,MICROSECOND,MILLISECOND,SECOND,MINUTE,HOUR,DAY,WEEK); variable-length units (MONTH,QUARTER,YEAR) raise an exception.
Returned value
For each row, the value is computed as:
0for the first row;0for any row whose elapsed time since the previous row is non-positive (that is, , as happens with descending order or duplicate timestamps); and- otherwise.
If the computed value would be negative, it is clamped to 0. The return type is Float64.
Example
The following example computes the per-second rate of change of a sensor reading.
Note that the third row drops from 110 to 105, so its derivative is clamped to 0.