Skip to content

Commit

Permalink
Fix procedure for counter aggs (github#526)
Browse files Browse the repository at this point in the history
* update markdown

* format text
  • Loading branch information
jacobprall authored Oct 19, 2021
1 parent fd53102 commit 9d3b6e0
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions timescaledb/how-to-guides/hyperfunctions/counter-aggs.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,37 @@ counter data.

### Running a counter aggregate query using a delta function
1. Create a table called `example`:
```sql
CREATE TABLE example (
measure_id BIGINT,
ts TIMESTAMPTZ ,
val DOUBLE PRECISION,
PRIMARY KEY (measure_id, ts)
);
```
1. Create a counter aggregate and the delta accessor function. This gives you
the change in the counter's value over the time period, accounting for any
resets. This allows you to search for fifteen minute periods where the
counter increased by a larger or smaller amount:
```sql
SELECT measure_id,
delta(
counter_agg(ts, val)
)
FROM example
GROUP BY measure_id;
```
1. You can also use the `time_bucket` function to produce a series of deltas over fifteen minute increments:
```sql
SELECT measure_id,
time_bucket('15 min'::interval, ts) as bucket,
delta(
counter_agg(ts, val)
)
FROM example
GROUP BY measure_id, time_bucket('15 min'::interval, ts);
```
```sql
CREATE TABLE example (
measure_id BIGINT,
ts TIMESTAMPTZ ,
val DOUBLE PRECISION,
PRIMARY KEY (measure_id, ts)
);
```
1. Create a counter aggregate and the delta accessor function. This gives you
the change in the counter's value over the time period, accounting for any resets.
This allows you to search for fifteen minute periods where the counter increased
by a larger or smaller amount:
```sql
SELECT measure_id,
delta(
counter_agg(ts, val)
)
FROM example
GROUP BY measure_id;
```
1. You can also use the `time_bucket` function to produce a series of deltas
over fifteen minute increments:
```sql
SELECT measure_id,
time_bucket('15 min'::interval, ts) as bucket,
delta(
counter_agg(ts, val)
)
FROM example
GROUP BY measure_id, time_bucket('15 min'::interval, ts);
```
</procedure>
Expand Down

0 comments on commit 9d3b6e0

Please sign in to comment.