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.
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
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.
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.
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.
Query
Query
Response