Given two tables patients and covid_tests, find patients who have recovered from COVID.
Recovery Definition: A patient is considered recovered if they have at least one Positive test followed by at least one Negative test on a later date.
Requirements:
- Calculate the recovery time in days as the difference between the first positive test and the first negative test after that positive test
- Only include patients who have both positive and negative test results
- Return results ordered by recovery_time ascending, then by patient_name ascending
Table Schema
| Column Name | Type | Description |
|---|---|---|
patient_id
PK
|
int | Unique identifier for each patient |
patient_name
|
varchar | Name of the patient |
age
|
int | Age of the patient |
| Column Name | Type | Description |
|---|---|---|
test_id
PK
|
int | Unique identifier for each test |
patient_id
|
int | Foreign key referencing patients table |
test_date
|
date | Date when the test was conducted |
result
|
varchar | Test result: Positive, Negative, or Inconclusive |
Input & Output
| patient_id | patient_name | age |
|---|---|---|
| 1 | Alice | 30 |
| 2 | Bob | 25 |
| 3 | Charlie | 35 |
| test_id | patient_id | test_date | result |
|---|---|---|---|
| 1 | 1 | 2024-01-01 | Positive |
| 2 | 1 | 2024-01-10 | Negative |
| 3 | 2 | 2024-01-05 | Positive |
| 4 | 2 | 2024-01-12 | Negative |
| 5 | 3 | 2024-01-03 | Negative |
| patient_name | first_positive_date | first_negative_date | recovery_time |
|---|---|---|---|
| Bob | 2024-01-05 | 2024-01-12 | 7 |
| Alice | 2024-01-01 | 2024-01-10 | 9 |
Alice tested positive on 2024-01-01 and negative on 2024-01-10 (9 days recovery). Bob tested positive on 2024-01-05 and negative on 2024-01-12 (7 days recovery). Charlie only has negative tests, so is not included. Results are ordered by recovery_time (7, 9) then by patient_name.
| patient_id | patient_name | age |
|---|---|---|
| 1 | David | 40 |
| 2 | Eve | 28 |
| test_id | patient_id | test_date | result |
|---|---|---|---|
| 1 | 1 | 2024-01-01 | Positive |
| 2 | 1 | 2024-01-05 | Positive |
| 3 | 1 | 2024-01-10 | Negative |
| 4 | 1 | 2024-01-15 | Negative |
| 5 | 2 | 2024-01-02 | Positive |
| patient_name | first_positive_date | first_negative_date | recovery_time |
|---|---|---|---|
| David | 2024-01-01 | 2024-01-10 | 9 |
David has multiple positive and negative tests. We take his first positive (2024-01-01) and first negative after that (2024-01-10) for 9 days recovery. Eve only has positive tests with no negative follow-up, so she's not included in the results.
| patient_id | patient_name | age |
|---|---|---|
| 1 | Frank | 45 |
| test_id | patient_id | test_date | result |
|---|---|---|---|
| 1 | 1 | 2024-01-01 | Negative |
| 2 | 1 | 2024-01-05 | Positive |
| 3 | 1 | 2024-01-03 | Negative |
| patient_name | first_positive_date | first_negative_date | recovery_time |
|---|
Frank has a negative test on 2024-01-03 that comes before his positive test on 2024-01-05, but no negative test after the positive. Since there's no negative test after the positive test, Frank is not considered recovered and doesn't appear in the results.
Constraints
-
1 ≤ patient_id ≤ 1000 -
1 ≤ test_id ≤ 10000 -
resultis one of'Positive','Negative', or'Inconclusive' -
test_dateis a valid date -
patient_namecontains only letters and spaces