Skip to content

Commit be65197

Browse files
committed
task: #3586
1 parent bfa6d52 commit be65197

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
190190
- [3521. Find Product Recommendation Pairs](./leetcode/medium/3521.%20Find%20Product%20Recommendation%20Pairs.sql)
191191
- [3564. Seasonal Sales Analysis](./leetcode/medium/3564.%20Seasonal%20Sales%20Analysis.sql)
192192
- [3580. Find Consistently Improving Employees](./leetcode/medium/3580.%20Find%20Consistently%20Improving%20Employees.sql)
193+
- [3586. Find COVID Recovery Patients](./leetcode/medium/3586.%20Find%20COVID%20Recovery%20Patients.sql)
193194
- [3601. Find Drivers with Improved Fuel Efficiency](./leetcode/medium/3601.%20Find%20Drivers%20with%20Improved%20Fuel%20Efficiency.sql)
194195
3. [Hard](./leetcode/hard/)
195196
- [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Question 3586. Find COVID Recovery Patients
3+
Link: https://leetcode.com/problems/find-covid-recovery-patients/description/?envType=problem-list-v2&envId=database
4+
5+
Table: patients
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| patient_id | int |
11+
| patient_name| varchar |
12+
| age | int |
13+
+-------------+---------+
14+
patient_id is the unique identifier for this table.
15+
Each row contains information about a patient.
16+
Table: covid_tests
17+
18+
+-------------+---------+
19+
| Column Name | Type |
20+
+-------------+---------+
21+
| test_id | int |
22+
| patient_id | int |
23+
| test_date | date |
24+
| result | varchar |
25+
+-------------+---------+
26+
test_id is the unique identifier for this table.
27+
Each row represents a COVID test result. The result can be Positive, Negative, or Inconclusive.
28+
Write a solution to find patients who have recovered from COVID - patients who tested positive but later tested negative.
29+
30+
A patient is considered recovered if they have at least one Positive test followed by at least one Negative test on a later date
31+
Calculate the recovery time in days as the difference between the first positive test and the first negative test after that positive test
32+
Only include patients who have both positive and negative test results
33+
Return the result table ordered by recovery_time in ascending order, then by patient_name in ascending order.
34+
*/
35+
36+
WITH first_positive AS (
37+
SELECT
38+
patient_id,
39+
MIN(test_date) AS positive
40+
FROM covid_tests
41+
WHERE result = 'Positive'
42+
GROUP BY patient_id
43+
),
44+
45+
first_negative AS (
46+
SELECT
47+
ct.patient_id,
48+
MIN(ct.test_date) AS negative,
49+
MIN(fp.positive) AS positive
50+
FROM covid_tests AS ct
51+
LEFT JOIN
52+
first_positive AS fp
53+
ON ct.patient_id = fp.patient_id
54+
WHERE ct.result = 'Negative' AND ct.test_date > fp.positive
55+
GROUP BY ct.patient_id
56+
)
57+
58+
SELECT
59+
fn.patient_id,
60+
p.patient_name,
61+
p.age,
62+
fn.negative - fn.positive AS recovery_time
63+
FROM first_negative AS fn
64+
LEFT JOIN
65+
patients AS p
66+
ON fn.patient_id = p.patient_id
67+
ORDER BY recovery_time ASC, p.patient_name ASC

0 commit comments

Comments
 (0)