Task
You are given Alabama Staffing integration event data and client account data. Write a PostgreSQL query to return all clients that have experienced more than three integration errors during the past 7 days. Only count events where the status is error, and include the client name, Alabama Staffing account manager, and the total number of qualifying errors. Order the results by error count descending, then client name ascending.
Schema
clients
| column | type | description |
|---|
| client_id | INT | Unique client identifier |
| client_name | VARCHAR(100) | Client name |
| account_manager | VARCHAR(100) | Alabama Staffing account manager |
| is_active | BOOLEAN | Whether the client account is active |
integrations
| column | type | description |
|---|
| integration_id | INT | Unique integration identifier |
| client_id | INT | Client tied to the integration |
| integration_name | VARCHAR(100) | Integration name |
| platform | VARCHAR(50) | Connected platform |
integration_events
| column | type | description |
|---|
| event_id | INT | Unique event identifier |
| integration_id | INT | Integration that produced the event |
| event_time | TIMESTAMP | Time of the event |
| status | VARCHAR(20) | Event status |
| error_code | VARCHAR(50) | Error code when applicable |
Sample data
Representative rows include clients with multiple integrations, recent and older errors, successful events, and one event with a NULL status.
Expected output
| client_id | client_name | account_manager | error_count |
|---|
| 1 | Northstar Health | Maya Patel | 5 |
| 5 | Cedar Logistics | Maya Patel | 4 |