Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for Consecutive Treatment Gaps

HardSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL for Consecutive Treatment Gaps. Start with the requirements and the one table on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Verily’s clinical research workflows need to identify periods when a patient had no recorded treatment activity. Given a patient event log, write a PostgreSQL query that finds consecutive treatment events separated by more than 90 days.

Requirements

  1. Consider only rows where event_type = 'treatment' and event_date is not NULL.
  2. Compare each treatment event with the immediately preceding treatment event for the same patient. Other event types, such as visits and lab results, must not reset the treatment sequence.
  3. Return the patient, the start and end dates of each qualifying gap, and the number of elapsed days.
  4. Include only gaps strictly greater than 90 days and order the results by patient and gap start date.

Schema

patient_event_log
ColumnTypeDescription
event_idPKINTUnique clinical event identifier
patient_idINTIdentifier of the patient associated with the event
event_typeVARCHAR(30)Clinical event classification, such as treatment, visit, or lab
event_dateDATEDate on which the clinical event occurred
Tablespatient_event_log
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results