Skip to content
Lucas Lessa
Voltar para as notícias
DatabricksGAImpacto: Médio

New counter_diff SQL window function for time-series analysis

The counter_diff window function calculates differences between consecutive cumulative counter values and handles resets.

O que mudou

The counter_diff window function converts cumulative counters into delta format by calculating the difference between consecutive values in a time series. ### Use cases Imagine you monitor system request volumes and need to turn cumulative readings into incremental interval values. It helps to: - Convert cumulative counters into deltas. - Handle counter drops when a service or machine restarts. - Use an optional timestamp to signal resets independently. ### How to use ```sql SELECT m, t, counter_diff(c) OVER (PARTITION BY m ORDER BY t) FROM VALUES ('http_requests', TIMESTAMP '2026-01-01T00:00:00', 100), ('http_requests', TIMESTAMP '2026-01-01T00:01:00', 200), ('http_requests', TIMESTAMP '2026-01-01T00:02:00', 400) AS tab(m, t, c);

What happens in this code:
1. The PARTITION BY clause separates data by metric name.
2. The ORDER BY clause sorts rows by timestamp.
3. The function calculates the difference between the current and previous counter value. ### Limitations and requirements Available in Databricks SQL and Databricks Runtime 19.0 and above. The function strictly requires an ORDER BY clause in the window definition. Negative values raise an error.
Ver nota oficial do Databricks

The counter_diff SQL window function is now available

Resumo e explicação gerados por IA a partir da release note oficial. Em caso de dúvida, a documentação oficial da plataforma prevalece.

Relacionadas