Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Pivot Diagnosis Counts by Category

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

Your question is Pivot Diagnosis Counts by Category. Start with the requirements and the four 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

Novo Nordisk needs a monthly view of diagnosed patients by clinical category for analytics reporting. Write a PostgreSQL query that pivots selected diagnosis categories into separate columns.

Requirements

  1. Include January through March 2025, including months with no qualifying diagnoses.
  2. Join patients, encounters, diagnosis events, and diagnosis categories.
  3. Include only completed encounters for patients who consented to analytics and whose country is Denmark.
  4. Count each patient at most once per month and diagnosis category, even if multiple diagnosis events exist.
  5. Return columns for Type 2 Diabetes, Obesity, Cardiovascular, and Rare Disease, plus the total number of distinct diagnosed patients per month.
  6. Order the output chronologically by month.

Schema

patients
ColumnTypeDescription
patient_idPKINTPatient identifier
patient_nameVARCHAR(100)Patient name
countryVARCHAR(80)Patient country
analytics_consentBOOLEANWhether analytics use is permitted
encounters
ColumnTypeDescription
encounter_idPKINTEncounter identifier
patient_idINTReferenced patient
encounter_dateDATEDate of encounter
statusVARCHAR(30)Encounter status
diagnosis_events
ColumnTypeDescription
diagnosis_event_idPKINTDiagnosis event identifier
encounter_idINTReferenced encounter
patient_idINTReferenced patient
category_idINTReferenced diagnosis category
diagnosis_codeVARCHAR(20)Clinical diagnosis code
diagnosis_categories
ColumnTypeDescription
category_idPKINTCategory identifier
category_nameVARCHAR(100)Clinical diagnosis category
therapy_areaVARCHAR(100)Broader therapy area
Tablespatientsencountersdiagnosis_eventsdiagnosis_categories
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results