Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
feat(docs): describe rate with window functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Dec 14, 2019
1 parent 75a8824 commit 1c1236c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions schema/patterns.md → docs/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,33 @@ FROM (
GROUP BY metric, time
ORDER BY 1, 2
```

## Window Functions

Window functions allow aggregates to view more than one row at a time, often using the previous and/or next row
to calculate change over time. Larger windows may calculate ordered aggregates like percentiles, but also need to
look at a larger set of rows and may not scale well.

Some utility functions are provided in [the `rate_` family](../schema/utils/rate.sql) to calculate change between
samples, handle counter resets, and adjust for time.

Time buckets should be run before the window function and may be done in a sub-select to help deduplicate:

```sql
SELECT
metric,
bucket AS time,
rate_time(value, lag(value) OVER w, '$__interval') AS value
FROM (
SELECT
CONCAT(labels->>'instance', labels->>'job') AS metric,
$__timeGroup("time", $__interval) AS bucket,
MAX(value) AS value
FROM metrics
WHERE
$__timeFilter("time") AND
name = 'some_metric'
) AS m
WINDOW w AS (PARTITION BY metric ORDER BY time)
ORDER BY metric, time;
```

0 comments on commit 1c1236c

Please sign in to comment.