Detecting ECG Tachycardia Runs
0:00:00
A digital health startup is analyzing ECG sensor data captured from wearable heart monitors. Each row in the dataset represents one heartbeat, including its timestamp and calculated heart rate at that moment.
The clinical team wants to detect tachycardia runs, periods where a patient’s heart rate stays above 100 bpm continuously for at least 10 seconds .
Write a SQL query to identify every valid tachycardia run and return:
patient_idrun_start: timestamp of the first beat in the runrun_end: timestamp of the last beat in the runduration_seconds: difference in seconds between start and end (rounded to 2 decimals)
Sort results by patient_id, then run_start.
Definition of a Tachycardia Run
- All consecutive heartbeats must have
heart_rate > 100 - Total duration ≥ 10 seconds
- Any gap ≥ 3 seconds between consecutive beats breaks the run
Note: A patient may have multiple separate runs.
Schema
Input
ecg_readings table
| Column | Type |
|---|---|
| patient_id | INTEGER |
| timestamp | DATETIME |
| heart_rate | INTEGER |
Output
| Column | Type |
|---|---|
| patient_id | INTEGER |
| run_start | DATETIME |
| run_end | DATETIME |
| duration_seconds | DECIMAL |
Example
Input
ecg_readings table
| patient_id | timestamp | heart_rate |
|---|---|---|
| 1 | 2025-03-01 10:00:00 | 98 |
| 1 | 2025-03-01 10:00:02 | 105 |
| 1 | 2025-03-01 10:00:04 | 110 |
| 1 | 2025-03-01 10:00:07 | 108 |
| 1 | 2025-03-01 10:00:12 | 107 |
| 1 | 2025-03-01 10:00:15 | 85 |
| 1 | 2025-03-01 10:00:20 | 120 |
| 1 | 2025-03-01 10:00:28 | 122 |
Output
| patient_id | run_start | run_end | duration_seconds |
|---|---|---|---|
| 1 | 2025-03-01 10:00:02 | 2025-03-01 10:00:12 | 10.00 |
Explanation
The high-rate streak from 10:00:02 to 10:00:12 forms one continuous sequence because each gap between timestamps (2 to 4, 4 to 7, and 7 to 12) is less than three seconds. This creates a valid 10-second run. The later high values at seconds 20 and 28 are separated by an eight-second gap, so they form a different sequence that is shorter than 10 seconds and is therefore ignored.
.
.
.
.
Comments