> ## Documentation Index
> Fetch the complete documentation index at: https://help.treble.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# fact_agent_status_changes

> One row per online/offline status change of an agent.

# fact\_agent\_status\_changes

Each row represents a status change (online/offline) of an agent on the agent platform.

## What questions does it answer?

* When did agents connect and disconnect?
* How many hours was each agent available?
* What was the connection schedule of each agent?

## Columns

| Column             | Type       | Description                                                                |
| ------------------ | ---------- | -------------------------------------------------------------------------- |
| `status_change_id` | Int32      | Unique event identifier                                                    |
| `company_id`       | Int32      | Company identifier (filtered automatically)                                |
| `agent_id`         | Int32      | Agent identifier                                                           |
| `agent_name`       | String     | Agent name                                                                 |
| `created_at`       | DateTime64 | Date and time of the status change                                         |
| `old_status`       | Bool       | Previous status. `true` = online, `false` = offline. `NULL` if first event |
| `new_status`       | Bool       | New status. `true` = online, `false` = offline                             |

## Example queries

### Agent connection schedule today

```sql theme={null}
SELECT
    agent_name,
    created_at,
    if(new_status, 'online', 'offline') AS status
FROM client_analytics.fact_agent_status_changes
WHERE created_at >= today()
ORDER BY agent_name, created_at
```

### Online minutes per agent per day

```sql theme={null}
SELECT
    agent_name,
    toDate(created_at) AS day,
    round(sum(if(new_status = true,
        dateDiff('minute', created_at,
            leadInFrame(created_at) OVER (PARTITION BY agent_id ORDER BY created_at)
        ), 0)) / 60, 1) AS online_hours
FROM client_analytics.fact_agent_status_changes
WHERE created_at >= now() - INTERVAL 7 DAY
GROUP BY agent_name, day
ORDER BY day DESC, agent_name
```
