---
title: compact_state_agg() | Tiger Data Docs
description: Aggregate state data into a state aggregate for further analysis
---

Early access [1.5.0](https://github.com/timescale/timescaledb-toolkit/releases/tag/1.5.0)

Aggregate a dataset containing state data into a state aggregate to track the time spent in each state.

## Samples

Create a state aggregate to track the status of some devices.

```
SELECT toolkit_experimental.compact_state_agg(time, status) FROM devices;
```

## Arguments

The syntax is:

```
compact_state_agg(
  ts TIMESTAMPTZ,
  value {TEXT | BIGINT}
) RETURNS CompactStateAgg
```

| Name  | Type        | Default | Required | Description                                   |
| ----- | ----------- | ------- | -------- | --------------------------------------------- |
| ts    | TIMESTAMPTZ | -       | ✔        | Timestamps associated with each state reading |
| value | TEXT        | BIGINT  | -        | ✔                                             |

## Returns

| Column | Type            | Description                                          |
| ------ | --------------- | ---------------------------------------------------- |
| agg    | CompactStateAgg | An object storing the total time spent in each state |
