From be65197a2e466023e3786c65eaeb8c749dd3e1c4 Mon Sep 17 00:00:00 2001 From: ivanbyone Date: Mon, 28 Jul 2025 23:30:50 +0300 Subject: [PATCH] task: #3586 --- README.md | 1 + .../3586. Find COVID Recovery Patients.sql | 67 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 leetcode/medium/3586. Find COVID Recovery Patients.sql diff --git a/README.md b/README.md index 78d4f5f..b1ad918 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/leetcode/medium/3586. Find COVID Recovery Patients.sql b/leetcode/medium/3586. Find COVID Recovery Patients.sql new file mode 100644 index 0000000..707adde --- /dev/null +++ b/leetcode/medium/3586. Find COVID Recovery Patients.sql @@ -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