{ "data": { "question": { "questionId": "3932", "questionFrontendId": "3586", "categoryTitle": "Database", "boundTopicId": 3701001, "title": "Find COVID Recovery Patients", "titleSlug": "find-covid-recovery-patients", "content": "

Table: patients

\n\n
\n+-------------+---------+\n| Column Name | Type    |\n+-------------+---------+\n| patient_id  | int     |\n| patient_name| varchar |\n| age         | int     |\n+-------------+---------+\npatient_id is the unique identifier for this table.\nEach row contains information about a patient.\n
\n\n

Table: covid_tests

\n\n
\n+-------------+---------+\n| Column Name | Type    |\n+-------------+---------+\n| test_id     | int     |\n| patient_id  | int     |\n| test_date   | date    |\n| result      | varchar |\n+-------------+---------+\ntest_id is the unique identifier for this table.\nEach row represents a COVID test result. The result can be Positive, Negative, or Inconclusive.\n
\n\n

Write a solution to find patients who have recovered from COVID - patients who tested positive but later tested negative.

\n\n\n\n

Return the result table ordered by recovery_time in ascending order, then by patient_name in ascending order.

\n\n

The result format is in the following example.

\n\n

 

\n

Example:

\n\n
\n

Input:

\n\n

patients table:

\n\n
\n+------------+--------------+-----+\n| patient_id | patient_name | age |\n+------------+--------------+-----+\n| 1          | Alice Smith  | 28  |\n| 2          | Bob Johnson  | 35  |\n| 3          | Carol Davis  | 42  |\n| 4          | David Wilson | 31  |\n| 5          | Emma Brown   | 29  |\n+------------+--------------+-----+\n
\n\n

covid_tests table:

\n\n
\n+---------+------------+------------+--------------+\n| test_id | patient_id | test_date  | result       |\n+---------+------------+------------+--------------+\n| 1       | 1          | 2023-01-15 | Positive     |\n| 2       | 1          | 2023-01-25 | Negative     |\n| 3       | 2          | 2023-02-01 | Positive     |\n| 4       | 2          | 2023-02-05 | Inconclusive |\n| 5       | 2          | 2023-02-12 | Negative     |\n| 6       | 3          | 2023-01-20 | Negative     |\n| 7       | 3          | 2023-02-10 | Positive     |\n| 8       | 3          | 2023-02-20 | Negative     |\n| 9       | 4          | 2023-01-10 | Positive     |\n| 10      | 4          | 2023-01-18 | Positive     |\n| 11      | 5          | 2023-02-15 | Negative     |\n| 12      | 5          | 2023-02-20 | Negative     |\n+---------+------------+------------+--------------+\n
\n\n

Output:

\n\n
\n+------------+--------------+-----+---------------+\n| patient_id | patient_name | age | recovery_time |\n+------------+--------------+-----+---------------+\n| 1          | Alice Smith  | 28  | 10            |\n| 3          | Carol Davis  | 42  | 10            |\n| 2          | Bob Johnson  | 35  | 11            |\n+------------+--------------+-----+---------------+\n
\n\n

Explanation:

\n\n\n\n

Output table is ordered by recovery_time in ascending order, and then by patient_name in ascending order.

\n
\n", "translatedTitle": "寻找 COVID 康复患者", "translatedContent": "

表:patients

\n\n
\n+-------------+---------+\n| Column Name | Type    |\n+-------------+---------+\n| patient_id  | int     |\n| patient_name| varchar |\n| age         | int     |\n+-------------+---------+\npatient_id 是这张表的唯一主键。\n每一行表示一个患者的信息。\n
\n\n

表:covid_tests

\n\n
\n+-------------+---------+\n| Column Name | Type    |\n+-------------+---------+\n| test_id     | int     |\n| patient_id  | int     |\n| test_date   | date    |\n| result      | varchar |\n+-------------+---------+\ntest_id 是这张表的唯一主键。\n每一行代表一个 COVID 检测结果。结果可以是阳性、阴性或不确定。\n
\n\n

编写一个解决方案以找到从 COVID 中康复的患者——那些曾经检测呈阳性但后来检测呈阴性的患者。

\n\n\n\n

返回结果表以 recovery_time 升序 排序,然后以 patient_name 升序 排序。

\n\n

结果格式如下所示。

\n\n

 

\n\n

示例:

\n\n
\n

输入:

\n\n

patients 表:

\n\n
\n+------------+--------------+-----+\n| patient_id | patient_name | age |\n+------------+--------------+-----+\n| 1          | Alice Smith  | 28  |\n| 2          | Bob Johnson  | 35  |\n| 3          | Carol Davis  | 42  |\n| 4          | David Wilson | 31  |\n| 5          | Emma Brown   | 29  |\n+------------+--------------+-----+\n
\n\n

covid_tests 表:

\n\n
\n+---------+------------+------------+--------------+\n| test_id | patient_id | test_date  | result       |\n+---------+------------+------------+--------------+\n| 1       | 1          | 2023-01-15 | Positive     |\n| 2       | 1          | 2023-01-25 | Negative     |\n| 3       | 2          | 2023-02-01 | Positive     |\n| 4       | 2          | 2023-02-05 | Inconclusive |\n| 5       | 2          | 2023-02-12 | Negative     |\n| 6       | 3          | 2023-01-20 | Negative     |\n| 7       | 3          | 2023-02-10 | Positive     |\n| 8       | 3          | 2023-02-20 | Negative     |\n| 9       | 4          | 2023-01-10 | Positive     |\n| 10      | 4          | 2023-01-18 | Positive     |\n| 11      | 5          | 2023-02-15 | Negative     |\n| 12      | 5          | 2023-02-20 | Negative     |\n+---------+------------+------------+--------------+\n
\n\n

输出:

\n\n
\n+------------+--------------+-----+---------------+\n| patient_id | patient_name | age | recovery_time |\n+------------+--------------+-----+---------------+\n| 1          | Alice Smith  | 28  | 10            |\n| 3          | Carol Davis  | 42  | 10            |\n| 2          | Bob Johnson  | 35  | 11            |\n+------------+--------------+-----+---------------+\n
\n\n

解释:

\n\n\n\n

输出表以 recovery_time 升序排序,然后以 patient_name 升序排序。

\n
\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 0, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false, \"cangjie\": false}", "topicTags": [ { "name": "Database", "slug": "database", "translatedName": "数据库", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef find_covid_recovery_patients(patients: pd.DataFrame, covid_tests: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"102\", \"totalSubmission\": \"180\", \"totalAcceptedRaw\": 102, \"totalSubmissionRaw\": 180, \"acRate\": \"56.7%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"patients\":[\"patient_id\",\"patient_name\",\"age\"],\"covid_tests\":[\"test_id\",\"patient_id\",\"test_date\",\"result\"]},\"rows\":{\"patients\":[[1,\"Alice Smith\",28],[2,\"Bob Johnson\",35],[3,\"Carol Davis\",42],[4,\"David Wilson\",31],[5,\"Emma Brown\",29]],\"covid_tests\":[[1,1,\"2023-01-15\",\"Positive\"],[2,1,\"2023-01-25\",\"Negative\"],[3,2,\"2023-02-01\",\"Positive\"],[4,2,\"2023-02-05\",\"Inconclusive\"],[5,2,\"2023-02-12\",\"Negative\"],[6,3,\"2023-01-20\",\"Negative\"],[7,3,\"2023-02-10\",\"Positive\"],[8,3,\"2023-02-20\",\"Negative\"],[9,4,\"2023-01-10\",\"Positive\"],[10,4,\"2023-01-18\",\"Positive\"],[11,5,\"2023-02-15\",\"Negative\"],[12,5,\"2023-02-20\",\"Negative\"]]}}", "metaData": "{\"mysql\":[\"CREATE TABLE patients (\\n patient_id INT,\\n patient_name VARCHAR(255),\\n age INT\\n)\",\"CREATE TABLE covid_tests (\\n test_id INT,\\n patient_id INT,\\n test_date DATE,\\n result VARCHAR(50)\\n)\"],\"mssql\":[\"CREATE TABLE patients (\\n patient_id INT,\\n patient_name VARCHAR(255),\\n age INT\\n)\",\"CREATE TABLE covid_tests (\\n test_id INT,\\n patient_id INT,\\n test_date DATE,\\n result VARCHAR(50)\\n)\"],\"oraclesql\":[\"CREATE TABLE patients (\\n patient_id NUMBER,\\n patient_name VARCHAR2(255),\\n age NUMBER\\n)\",\"CREATE TABLE covid_tests (\\n test_id NUMBER,\\n patient_id NUMBER,\\n test_date DATE,\\n result VARCHAR2(50)\\n)\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"],\"database\":true,\"name\":\"find_covid_recovery_patients\",\"postgresql\":[\"CREATE TABLE patients (\\n patient_id INTEGER,\\n patient_name VARCHAR(255),\\n age INTEGER\\n);\\n\",\"CREATE TABLE covid_tests (\\n test_id INTEGER,\\n patient_id INTEGER,\\n test_date DATE,\\n result VARCHAR(50)\\n);\\n\"],\"pythondata\":[\"patients = pd.DataFrame({\\n 'patient_id': pd.Series(dtype='int'),\\n 'patient_name': pd.Series(dtype='str'),\\n 'age': pd.Series(dtype='int')\\n})\",\"covid_tests = pd.DataFrame({\\n 'test_id': pd.Series(dtype='int'),\\n 'patient_id': pd.Series(dtype='int'),\\n 'test_date': pd.Series(dtype='datetime64[ns]'),\\n 'result': pd.Series(dtype='str')\\n})\"],\"database_schema\":{\"patients\":{\"patient_id\":\"INT\",\"patient_name\":\"VARCHAR(255)\",\"age\":\"INT\"},\"covid_tests\":{\"test_id\":\"INT\",\"patient_id\":\"INT\",\"test_date\":\"DATE\",\"result\":\"VARCHAR(50)\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "CREATE TABLE patients (\n patient_id INT,\n patient_name VARCHAR(255),\n age INT\n)", "CREATE TABLE covid_tests (\n test_id INT,\n patient_id INT,\n test_date DATE,\n result VARCHAR(50)\n)", "Truncate table patients", "insert into patients (patient_id, patient_name, age) values ('1', 'Alice Smith', '28')", "insert into patients (patient_id, patient_name, age) values ('2', 'Bob Johnson', '35')", "insert into patients (patient_id, patient_name, age) values ('3', 'Carol Davis', '42')", "insert into patients (patient_id, patient_name, age) values ('4', 'David Wilson', '31')", "insert into patients (patient_id, patient_name, age) values ('5', 'Emma Brown', '29')", "Truncate table covid_tests", "insert into covid_tests (test_id, patient_id, test_date, result) values ('1', '1', '2023-01-15', 'Positive')", "insert into covid_tests (test_id, patient_id, test_date, result) values ('2', '1', '2023-01-25', 'Negative')", "insert into covid_tests (test_id, patient_id, test_date, result) values ('3', '2', '2023-02-01', 'Positive')", "insert into covid_tests (test_id, patient_id, test_date, result) values ('4', '2', '2023-02-05', 'Inconclusive')", "insert into covid_tests (test_id, patient_id, test_date, result) values ('5', '2', '2023-02-12', 'Negative')", "insert into covid_tests (test_id, patient_id, test_date, result) values ('6', '3', '2023-01-20', 'Negative')", "insert into covid_tests (test_id, patient_id, test_date, result) values ('7', '3', '2023-02-10', 'Positive')", "insert into covid_tests (test_id, patient_id, test_date, result) values ('8', '3', '2023-02-20', 'Negative')", "insert into covid_tests (test_id, patient_id, test_date, result) values ('9', '4', '2023-01-10', 'Positive')", "insert into covid_tests (test_id, patient_id, test_date, result) values ('10', '4', '2023-01-18', 'Positive')", "insert into covid_tests (test_id, patient_id, test_date, result) values ('11', '5', '2023-02-15', 'Negative')", "insert into covid_tests (test_id, patient_id, test_date, result) values ('12', '5', '2023-02-20', 'Negative')" ], "enableRunCode": true, "envInfo": "{\"mysql\":[\"MySQL\",\"

\\u7248\\u672c\\uff1aMySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"

mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"

Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\"

Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\"

PostgreSQL 16<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "{\"headers\":{\"patients\":[\"patient_id\",\"patient_name\",\"age\"],\"covid_tests\":[\"test_id\",\"patient_id\",\"test_date\",\"result\"]},\"rows\":{\"patients\":[[1,\"Alice Smith\",28],[2,\"Bob Johnson\",35],[3,\"Carol Davis\",42],[4,\"David Wilson\",31],[5,\"Emma Brown\",29]],\"covid_tests\":[[1,1,\"2023-01-15\",\"Positive\"],[2,1,\"2023-01-25\",\"Negative\"],[3,2,\"2023-02-01\",\"Positive\"],[4,2,\"2023-02-05\",\"Inconclusive\"],[5,2,\"2023-02-12\",\"Negative\"],[6,3,\"2023-01-20\",\"Negative\"],[7,3,\"2023-02-10\",\"Positive\"],[8,3,\"2023-02-20\",\"Negative\"],[9,4,\"2023-01-10\",\"Positive\"],[10,4,\"2023-01-18\",\"Positive\"],[11,5,\"2023-02-15\",\"Negative\"],[12,5,\"2023-02-20\",\"Negative\"]]}}", "__typename": "QuestionNode" } } }