Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
- [3521. Find Product Recommendation Pairs](./leetcode/medium/3521.%20Find%20Product%20Recommendation%20Pairs.sql)
- [3564. Seasonal Sales Analysis](./leetcode/medium/3564.%20Seasonal%20Sales%20Analysis.sql)
- [3580. Find Consistently Improving Employees](./leetcode/medium/3580.%20Find%20Consistently%20Improving%20Employees.sql)
- [3586. Find COVID Recovery Patients](./leetcode/medium/3586.%20Find%20COVID%20Recovery%20Patients.sql)
- [3601. Find Drivers with Improved Fuel Efficiency](./leetcode/medium/3601.%20Find%20Drivers%20with%20Improved%20Fuel%20Efficiency.sql)
3. [Hard](./leetcode/hard/)
- [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql)
Expand Down
67 changes: 67 additions & 0 deletions leetcode/medium/3586. Find COVID Recovery Patients.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Question 3586. Find COVID Recovery Patients
Link: https://leetcode.com/problems/find-covid-recovery-patients/description/?envType=problem-list-v2&envId=database

Table: patients

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| patient_id | int |
| patient_name| varchar |
| age | int |
+-------------+---------+
patient_id is the unique identifier for this table.
Each row contains information about a patient.
Table: covid_tests

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| test_id | int |
| patient_id | int |
| test_date | date |
| result | varchar |
+-------------+---------+
test_id is the unique identifier for this table.
Each row represents a COVID test result. The result can be Positive, Negative, or Inconclusive.
Write a solution to find patients who have recovered from COVID - patients who tested positive but later tested negative.

A patient is considered recovered if they have at least one Positive test followed by at least one Negative test on a later date
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 the result table ordered by recovery_time in ascending order, then by patient_name in ascending order.
*/

WITH first_positive AS (
SELECT
patient_id,
MIN(test_date) AS positive
FROM covid_tests
WHERE result = 'Positive'
GROUP BY patient_id
),

first_negative AS (
SELECT
ct.patient_id,
MIN(ct.test_date) AS negative,
MIN(fp.positive) AS positive
FROM covid_tests AS ct
LEFT JOIN
first_positive AS fp
ON ct.patient_id = fp.patient_id
WHERE ct.result = 'Negative' AND ct.test_date > fp.positive
GROUP BY ct.patient_id
)

SELECT
fn.patient_id,
p.patient_name,
p.age,
fn.negative - fn.positive AS recovery_time
FROM first_negative AS fn
LEFT JOIN
patients AS p
ON fn.patient_id = p.patient_id
ORDER BY recovery_time ASC, p.patient_name ASC