|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 3586\. Find COVID Recovery Patients |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +Table: `patients` |
| 9 | + |
| 10 | + +-------------+---------+ |
| 11 | + | Column Name | Type | |
| 12 | + +-------------+---------+ |
| 13 | + | patient_id | int | |
| 14 | + | patient_name| varchar | |
| 15 | + | age | int | |
| 16 | + +-------------+---------+ |
| 17 | + patient_id is the unique identifier for this table. |
| 18 | + Each row contains information about a patient. |
| 19 | + |
| 20 | +Table: `covid_tests` |
| 21 | + |
| 22 | + +-------------+---------+ |
| 23 | + | Column Name | Type | |
| 24 | + +-------------+---------+ |
| 25 | + | test_id | int | |
| 26 | + | patient_id | int | |
| 27 | + | test_date | date | |
| 28 | + | result | varchar | |
| 29 | + +-------------+---------+ |
| 30 | + test_id is the unique identifier for this table. |
| 31 | + Each row represents a COVID test result. The result can be Positive, Negative, or Inconclusive. |
| 32 | + |
| 33 | +Write a solution to find patients who have **recovered from COVID** - patients who tested positive but later tested negative. |
| 34 | + |
| 35 | +* A patient is considered recovered if they have **at least one** **Positive** test followed by at least one **Negative** test on a **later date** |
| 36 | +* Calculate the **recovery time** in days as the **difference** between the **first positive test** and the **first negative test** after that **positive test** |
| 37 | +* **Only include** patients who have both positive and negative test results |
| 38 | + |
| 39 | +Return _the result table ordered by_ `recovery_time` _in **ascending** order, then by_ `patient_name` _in **ascending** order_. |
| 40 | + |
| 41 | +The result format is in the following example. |
| 42 | + |
| 43 | +**Example:** |
| 44 | + |
| 45 | +**Input:** |
| 46 | + |
| 47 | +patients table: |
| 48 | + |
| 49 | + +------------+--------------+-----+ |
| 50 | + | patient_id | patient_name | age | |
| 51 | + +------------+--------------+-----+ |
| 52 | + | 1 | Alice Smith | 28 | |
| 53 | + | 2 | Bob Johnson | 35 | |
| 54 | + | 3 | Carol Davis | 42 | |
| 55 | + | 4 | David Wilson | 31 | |
| 56 | + | 5 | Emma Brown | 29 | |
| 57 | + +------------+--------------+-----+ |
| 58 | + |
| 59 | +covid\_tests table: |
| 60 | + |
| 61 | + +---------+------------+------------+--------------+ |
| 62 | + | test_id | patient_id | test_date | result | |
| 63 | + |---------|------------|------------|--------------| |
| 64 | + | 1 | 1 | 2023-01-15 | Positive | |
| 65 | + | 2 | 1 | 2023-01-25 | Negative | |
| 66 | + | 3 | 2 | 2023-02-01 | Positive | |
| 67 | + | 4 | 2 | 2023-02-05 | Inconclusive | |
| 68 | + | 5 | 2 | 2023-02-12 | Negative | |
| 69 | + | 6 | 3 | 2023-01-20 | Negative | |
| 70 | + | 7 | 3 | 2023-02-10 | Positive | |
| 71 | + | 8 | 3 | 2023-02-20 | Negative | |
| 72 | + | 9 | 4 | 2023-01-10 | Positive | |
| 73 | + | 10 | 4 | 2023-01-18 | Positive | |
| 74 | + | 11 | 5 | 2023-02-15 | Negative | |
| 75 | + | 12 | 5 | 2023-02-20 | Negative | |
| 76 | + +---------+------------+------------+--------------+ |
| 77 | + |
| 78 | +**Output:** |
| 79 | + |
| 80 | + +------------+--------------+-----+---------------+ |
| 81 | + | patient_id | patient_name | age | recovery_time | |
| 82 | + |------------|--------------|-----|---------------| |
| 83 | + | 1 | Alice Smith | 28 | 10 | |
| 84 | + | 3 | Carol Davis | 42 | 10 | |
| 85 | + | 2 | Bob Johnson | 35 | 11 | |
| 86 | + +------------+--------------+-----+---------------+ |
| 87 | + |
| 88 | +**Explanation:** |
| 89 | + |
| 90 | +* **Alice Smith (patient\_id = 1):** |
| 91 | + * First positive test: 2023-01-15 |
| 92 | + * First negative test after positive: 2023-01-25 |
| 93 | + * Recovery time: 25 - 15 = 10 days |
| 94 | +* **Bob Johnson (patient\_id = 2):** |
| 95 | + * First positive test: 2023-02-01 |
| 96 | + * Inconclusive test on 2023-02-05 (ignored for recovery calculation) |
| 97 | + * First negative test after positive: 2023-02-12 |
| 98 | + * Recovery time: 12 - 1 = 11 days |
| 99 | +* **Carol Davis (patient\_id = 3):** |
| 100 | + * Had negative test on 2023-01-20 (before positive test) |
| 101 | + * First positive test: 2023-02-10 |
| 102 | + * First negative test after positive: 2023-02-20 |
| 103 | + * Recovery time: 20 - 10 = 10 days |
| 104 | +* **Patients not included:** |
| 105 | + * David Wilson (patient\_id = 4): Only has positive tests, no negative test after positive |
| 106 | + * Emma Brown (patient\_id = 5): Only has negative tests, never tested positive |
| 107 | + |
| 108 | +Output table is ordered by recovery\_time in ascending order, and then by patient\_name in ascending order. |
| 109 | + |
| 110 | +## Solution |
| 111 | + |
| 112 | +```sql |
| 113 | +# Write your MySQL query statement below |
| 114 | +-- mysql |
| 115 | +-- SELECT |
| 116 | +-- p.patient_id, |
| 117 | +-- p.patient_name, |
| 118 | +-- p.age, |
| 119 | +-- DATEDIFF( |
| 120 | +-- min(neg.test_date), |
| 121 | +-- min(pos.test_date) |
| 122 | +-- ) AS recovery_time |
| 123 | +-- FROM |
| 124 | +-- patients p |
| 125 | +-- JOIN covid_tests pos ON |
| 126 | +-- p.patient_id = pos.patient_id AND pos.result = 'Positive' |
| 127 | +-- JOIN covid_tests neg ON |
| 128 | +-- p.patient_id = neg.patient_id AND neg.result = 'Negative' |
| 129 | +-- WHERE |
| 130 | +-- neg.test_date > pos.test_date |
| 131 | +-- GROUP BY |
| 132 | +-- p.patient_id, p.patient_name, p.age |
| 133 | +-- ORDER BY |
| 134 | +-- recovery_time, p.patient_name; |
| 135 | +select |
| 136 | + p.patient_id, |
| 137 | + p.patient_name, |
| 138 | + p.age, |
| 139 | + datediff( |
| 140 | + day, |
| 141 | + pos.first_pos_date, |
| 142 | + neg.first_neg_date |
| 143 | + ) as recovery_time |
| 144 | +from |
| 145 | + patients p |
| 146 | + join ( |
| 147 | + select patient_id, min(test_date) as first_pos_date |
| 148 | + from covid_tests |
| 149 | + where result = 'Positive' |
| 150 | + group by patient_id |
| 151 | + ) pos on p.patient_id = pos.patient_id |
| 152 | + join ( |
| 153 | + select |
| 154 | + c1.patient_id, |
| 155 | + min(c1.test_date) as first_neg_date |
| 156 | + from |
| 157 | + covid_tests c1 |
| 158 | + join ( |
| 159 | + select patient_id, min(test_date) as first_pos_date |
| 160 | + from covid_tests |
| 161 | + where result = 'Positive' |
| 162 | + group by patient_id |
| 163 | + ) p2 on c1.patient_id = p2.patient_id |
| 164 | + where |
| 165 | + c1.result = 'Negative' |
| 166 | + and c1.test_date > p2.first_pos_date |
| 167 | + group by c1.patient_id |
| 168 | + ) neg on p.patient_id = neg.patient_id |
| 169 | +order by |
| 170 | + recovery_time ASC, p.patient_name ASC; |
| 171 | +``` |
0 commit comments