Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Retention Rates Across Sites

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

Your question is Rolling Retention Rates Across Sites. Start with the requirements and the two tables 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 Baseline study operations need to compare patient retention across clinical sites. A patient is retained for a visit date if the same patient has another visit at the same site within the following 30 calendar days.

Write a PostgreSQL query that calculates rolling 30-day retention by site and active visit date.

Requirements

  1. Count distinct active patients for each site and visit date.
  2. Count distinct retained patients whose subsequent visit occurs after the active date and within 30 days.
  3. Use a window function to calculate the retention rate across the current date and the previous 29 calendar days for each site.
  4. Return only rows with a non-null visit date, ordered by site and date. Round the retention rate to four decimal places.

Schema

sites
ColumnTypeDescription
site_idPKINTEGERClinical site identifier
site_nameVARCHAR(100)Clinical site name
regionVARCHAR(50)Geographic region of the site
patient_visits
ColumnTypeDescription
visit_idPKINTEGERUnique visit identifier
site_idINTEGERSite where the visit occurred
patient_idINTEGERPatient identifier
visit_dateDATEDate of the patient visit
Tablessitespatient_visits
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results