MIMIC-IV Patient Analysis
Perform a comprehensive, systematic analysis of a patient's complete clinical record from the MIMIC-IV database by querying the SQLite database directly and efficiently.
Database Structure
The database has 27 tables. Key tables and their primary columns:
Core patient tables:
hosp_patients — demographics: subject_id, gender, anchor_age, anchor_year, anchor_year_group, dod
hosp_admissions — hospital stays: subject_id, hadm_id, admittime, dischtime, deathtime, admission_type, admission_location, discharge_location, insurance, language, marital_status, race, edregtime, edouttime, hospital_expire_flag
Clinical data (per admission):
hosp_diagnoses_icd — ICD diagnoses: subject_id, hadm_id, seq_num, icd_code, icd_version
hosp_d_icd_diagnoses — diagnosis dictionary: icd_code, icd_version, long_title
hosp_procedures_icd — ICD procedures: subject_id, hadm_id, seq_num, chartdate, icd_code, icd_version
hosp_d_icd_procedures — procedure dictionary: icd_code, icd_version, long_title
hosp_drgcodes — DRG billing: subject_id, hadm_id, drg_type, drg_code, description, drg_severity, drg_mortality
hosp_services — clinical service: subject_id, hadm_id, transfertime, prev_service, curr_service
hosp_transfers — unit movements: subject_id, hadm_id, transfer_id, eventtype, careunit, intime, outtime
Medications:
hosp_prescriptions — prescribed drugs: subject_id, hadm_id, starttime, stoptime, drug, drug_type, dose_val_rx, dose_unit_rx, route
hosp_emar — administration record: subject_id, hadm_id, emar_id, charttime, medication, event_txt, scheduletime
hosp_pharmacy — pharmacy fills: subject_id, hadm_id, pharmacy_id, medication, starttime, stoptime (column is medication, NOT drug)
Diagnostics:
hosp_microbiologyevents — cultures: subject_id, hadm_id, charttime, spec_type_desc, test_name, org_name, interpretation, comments
hosp_omr — vitals/anthropometrics: subject_id, chartdate, seq_num, result_name, result_value
hosp_hcpcsevents — billing codes: subject_id, hadm_id, chartdate, hcpcs_cd, short_description
hosp_d_hcpcs — HCPCS dictionary: code, category, long_description, short_description
Orders:
hosp_poe — provider orders: subject_id, hadm_id, poe_id, ordertime, order_type, order_subtype, transaction_type, order_status
ICU tables (only present if patient had ICU stay):
icu_icustays — ICU episodes: subject_id, hadm_id, stay_id, first_careunit, last_careunit, intime, outtime, los
icu_inputevents — IV fluids/medications: stay_id, starttime, endtime, itemid, amount, amountuom, ordercategoryname
icu_outputevents — urine/drainage: stay_id, charttime, itemid, value, valueuom
icu_procedureevents — ICU procedures: stay_id, starttime, endtime, itemid, value, valueuom, ordercategoryname
icu_d_items — ICU item dictionary: itemid, label, category
Critical Column Name Pitfalls
| Table |
WRONG |
CORRECT |
hosp_transfers |
transfertime |
intime |
hosp_poe |
order_time |
ordertime |
hosp_omr |
charttime |
chartdate |
hosp_pharmacy |
drug |
medication |
hosp_hcpcsevents JOIN hosp_d_hcpcs |
ON h.hcpcs_cd = d.hcpcs_cd |
ON h.hcpcs_cd = d.code |
Analysis Workflow
Start with get_database_info to confirm table availability, then query directly — do not call describe_table before each query; use the column names listed above.
Step 1 — Patient demographics
SELECT * FROM hosp_patients WHERE subject_id = <patient_id>
anchor_age is age in anchor_year (dates are shifted for privacy). If dod is not null, the patient died.
Step 2 — All hospital admissions
SELECT * FROM hosp_admissions WHERE subject_id = <patient_id> ORDER BY admittime
For each hadm_id, note: admission/discharge times, type, source, destination, insurance, hospital_expire_flag.
If dod is not null, compute time from last discharge to death:
last_dischtime to dod in days = how long the patient survived after final hospitalization.
For multi-admission patients, compute readmission intervals:
SELECT hadm_id, admittime, dischtime,
CAST((julianday(admittime) - julianday(LAG(dischtime) OVER (ORDER BY admittime))) AS INTEGER) AS days_since_last_discharge
FROM hosp_admissions WHERE subject_id = <patient_id>
ORDER BY admittime
Track discharge destination progression (HOME → HOME HEALTH → SNF → LTACH → hospital death) as a functional decline signal.
Step 3 — ICU stays
SELECT * FROM icu_icustays WHERE subject_id = <patient_id> ORDER BY intime
Empty result = no ICU. If ICU present, note care units and length of stay (los).
Step 3.5 — ICU deep dive (when ICU stays exist)
For patients with ≤3 ICU stays, query all stay_ids. For >3 stays, prioritize by longest los and highest severity, covering at least the top 3.
For each stay_id, query in this order:
-- 1. Procedures (ventilation, dialysis, invasive lines)
SELECT pe.starttime, pe.endtime, d.label, d.category, pe.value, pe.valueuom
FROM icu_procedureevents pe
JOIN icu_d_items d ON pe.itemid = d.itemid
WHERE pe.stay_id = <stay_id>
ORDER BY pe.starttime
-- 2. Inputs (fluids, medications, blood products, nutrition)
SELECT ie.starttime, d.label, d.category, ie.amount, ie.amountuom, ie.ordercategoryname
FROM icu_inputevents ie
JOIN icu_d_items d ON ie.itemid = d.itemid
WHERE ie.stay_id = <stay_id>
ORDER BY ie.starttime LIMIT 50
-- 3. Outputs (urine, drainage)
SELECT oe.charttime, d.label, oe.value, oe.valueuom
FROM icu_outputevents oe
JOIN icu_d_items d ON oe.itemid = d.itemid
WHERE oe.stay_id = <stay_id>
ORDER BY oe.charttime LIMIT 30
For each ICU stay, extract and summarize:
- Ventilation: mechanical ventilation duration (label contains "Invasive Ventilation" or "Ventilation")
- Vasopressors: Norepinephrine, Epinephrine, Vasopressin, Phenylephrine, Dopamine (total amount)
- Sedation/analgesia: Propofol, Fentanyl, Midazolam, Dexmedetomidine
- Blood products: Packed RBCs, Platelets, FFP, Cryoprecipitate (
ordercategoryname = 'Blood Products')
- Enteral nutrition: formula names and total volumes (labels containing "Enteral", "Glucerna", "Promote", "Two Cal")
- Fluid balance: sum inputs (mL) minus sum outputs (mL) — positive = net accumulation
- Vascular access: arterial line, central line, Foley durations
Paginate inputs with OFFSET if >50 rows — blood products and nutrition often appear later.
Step 4 — Diagnoses (with human-readable names)
SELECT d.icd_code, d.icd_version, d.long_title, diag.hadm_id, diag.seq_num
FROM hosp_diagnoses_icd diag
JOIN hosp_d_icd_diagnoses d ON diag.icd_code = d.icd_code AND diag.icd_version = d.icd_version
WHERE diag.subject_id = <patient_id>
ORDER BY diag.hadm_id, diag.seq_num
seq_num=1 is the primary diagnosis. Note total diagnosis count per admission (high count = high complexity).
For patients with 4+ admissions, identify recurring diagnoses:
SELECT d.long_title, COUNT(*) as admission_count
FROM hosp_diagnoses_icd diag
JOIN hosp_d_icd_diagnoses d ON diag.icd_code = d.icd_code AND diag.icd_version = d.icd_version
WHERE diag.subject_id = <patient_id> AND diag.seq_num <= 5
GROUP BY d.long_title
ORDER BY admission_count DESC
LIMIT 20
Always query for special ICD codes — clinically critical status flags:
SELECT d.icd_code, d.long_title, diag.hadm_id
FROM hosp_diagnoses_icd diag
JOIN hosp_d_icd_diagnoses d ON diag.icd_code = d.icd_code AND diag.icd_version = d.icd_version
WHERE diag.subject_id = <patient_id>
AND (d.icd_code LIKE 'Z88%' -- drug allergies
OR d.icd_code = 'Z66' -- do not resuscitate
OR d.icd_code = 'Z515' -- palliative care
OR d.icd_code LIKE 'Z79%') -- long-term medication use
ORDER BY diag.hadm_id
Step 5 — Procedures
SELECT p.hadm_id, p.seq_num, p.chartdate, p.icd_code, proc.long_title
FROM hosp_procedures_icd p
JOIN hosp_d_icd_procedures proc ON p.icd_code = proc.icd_code AND p.icd_version = proc.icd_version
WHERE p.subject_id = <patient_id>
ORDER BY p.hadm_id, p.seq_num
Step 6 — Medications prescribed
SELECT drug, COUNT(*) as prescription_count
FROM hosp_prescriptions
WHERE subject_id = <patient_id>
GROUP BY drug
ORDER BY prescription_count DESC
LIMIT 20
Then per-admission detail:
SELECT hadm_id, drug, starttime, stoptime, dose_val_rx, dose_unit_rx, route
FROM hosp_prescriptions
WHERE subject_id = <patient_id>
ORDER BY hadm_id, starttime
Group medications by clinical class: anticoagulants/antiplatelets, cardiovascular, diuretics, analgesics/opioids, antibiotics, psychiatric agents, immunosuppressants. Note route transitions (IV → PO = improvement; PO/NG = nasogastric feeding from dysphagia). Multiple laxatives (Senna + Bisacodyl + Docusate) indicate immobility or opioid use.
Step 6.5 — Pharmacy fills (cross-validation)
SELECT hadm_id, medication, starttime, stoptime
FROM hosp_pharmacy
WHERE subject_id = <patient_id>
ORDER BY hadm_id, starttime
LIMIT 50
Column is medication, not drug. Paginate with OFFSET if needed.
Step 7 — Physical measurements (BMI, weight, height, BP)
Query all OMR data in a single consolidated query:
SELECT chartdate, result_name, result_value
FROM hosp_omr
WHERE subject_id = <patient_id>
ORDER BY chartdate
Synthesize trends, do not report individual data points. For longitudinal patients with many OMR records, summarize the overall trajectory:
- Weight: report baseline, minimum, maximum, and final values with percent change. Flag ≥5% loss as clinically significant, ≥10% as cachexia risk.
- BP: report overall range (systolic and diastolic) and note any hypertensive spikes (>140/90) or hypotensive episodes (<90/60).
- BMI: report range and trend direction.
- Flag implausible values (e.g., weight of 1731 lbs is likely a typo for 173.1).
Do NOT issue multiple paginated queries stepping through OMR records month-by-month or year-by-year. One query with a single synthesized trend summary is sufficient and prevents wasting analysis effort on granular data recitation.
Step 8 — Microbiology cultures
Query all microbiology data in a single consolidated query:
SELECT chartdate, spec_type_desc, test_name, org_name, interpretation, comments
FROM hosp_microbiologyevents
WHERE subject_id = <patient_id>
ORDER BY chartdate
Focus on clinically actionable findings:
- Positive cultures: Record organism name, specimen type, and antibiotic sensitivity (R/S/I).
- MRSA colonization: Note any MRSA screen positive results.
- Negative cultures: Summarize as "X cultures negative" rather than listing each one.
- Comments containing "< 10,000 CFU/mL" or "NO GROWTH" = negative.
- Comments containing "MIXED BACTERIAL FLORA" = likely contamination, not a true infection.
Paginate with OFFSET only if >50 results. Do NOT issue per-admission microbiology queries individually.
Step 9 — Clinical service and transfers
SELECT * FROM hosp_services WHERE subject_id = <patient_id> ORDER BY transfertime
SELECT hadm_id, eventtype, careunit, intime, outtime
FROM hosp_transfers WHERE subject_id = <patient_id>
ORDER BY hadm_id, intime
LIMIT 50
For patients with many admissions, paginate transfers with LIMIT/OFFSET.
Step 10 — DRG billing codes
SELECT * FROM hosp_drgcodes WHERE subject_id = <patient_id>
APR-DRG has severity (1-4) and mortality (1-4) scores. Severity 3-4 or mortality 3-4 = high-complexity/high-risk admission.
Step 11 — HCPCS events
SELECT h.hadm_id, h.chartdate, h.hcpcs_cd, d.short_description
FROM hosp_hcpcsevents h
JOIN hosp_d_hcpcs d ON h.hcpcs_cd = d.code
WHERE h.subject_id = <patient_id>
ORDER BY h.chartdate
Zero results = no billed procedures (common). G0378 = observation-status admission.
Step 12 — eMAR and Provider Orders
-- Medication administration record
SELECT charttime, medication, event_txt, scheduletime
FROM hosp_emar WHERE subject_id = <patient_id>
ORDER BY charttime LIMIT 50
-- Provider orders overview
SELECT order_type, COUNT(*) as order_count
FROM hosp_poe WHERE subject_id = <patient_id>
GROUP BY order_type
ORDER BY order_count DESC
-- Detailed orders for specific admission (when clinically relevant)
SELECT ordertime, order_type, order_subtype, transaction_type, order_status
FROM hosp_poe WHERE subject_id = <patient_id> AND hadm_id = <hadm_id>
ORDER BY ordertime LIMIT 30
The event_txt field distinguishes "Administered" from "Not Given" — this reveals medication compliance and route changes. Patterns of held critical drugs (immunosuppressants, anticoagulants) are adherence risk signals. "Not Given per Sliding Scale" for insulin is expected, not a compliance issue.
Order type distribution (Medications / Lab / Radiology / Nutrition / General Care) characterizes admission intensity. Rehabilitation consults (Speech/Swallowing, Occupational Therapy, Physical Therapy) signal functional impairment workup.
Producing the Final Report
After gathering data, produce a structured report with markdown headers and tables. The report must be comprehensive enough to answer detailed clinical questions about any aspect of the patient's care.
Required report sections:
Demographics — age, sex, race, insurance, vital status (alive/deceased + date); if deceased, compute days from last discharge to death; note insurance transitions (Private→Medicare = age 65)
Admission Summary — number of admissions, date range, types/sources; present as a markdown table for multi-admission patients with columns: hadm_id, admit date, discharge date, type, primary diagnosis, discharge location, LOS; computed readmission intervals highlighting any <30-day readmissions
ICU Course — whether ICU was needed, which units, LOS per stay; key interventions: ventilation duration, vasopressors (drug names + amounts), sedation agents, blood products (type + volume), enteral nutrition, fluid balance (total inputs − outputs mL)
Primary Diagnoses by Admission — primary condition per hadm_id; total diagnosis count per admission to indicate complexity; identify highest-complexity admissions
Comorbidities — significant secondary diagnoses; for multi-admission patients, note recurring conditions with frequency counts
Procedures — surgical and therapeutic interventions with dates; link to relevant admissions
Medications — organized by clinical class (anticoagulants, cardiovascular, diuretics, analgesics, antibiotics, immunosuppressants); for multi-admission patients, top prescriptions by frequency; route transitions and polypharmacy patterns; specific dose information for key drugs
Diagnostics — positive culture results (organism + specimen + sensitivity pattern); MRSA colonization status; physical measurement trends (summarized as ranges and percent changes, NOT individual data points); negative culture summary
Clinical Service Trajectory — services and care unit progression; transition patterns (e.g., Trauma SICU → Neuro SICU → Med/Surg = recovery)
Key Clinical Insights — clinically meaningful patterns with specific evidence:
- Discharge to rehab/SNF → functional impairment
- Multiple laxatives (Senna + Bisacodyl + Docusate) → immobility or opioid use
- PO/NG drug routes → nasogastric feeding (dysphagia)
- Sequential anticoagulant changes → treatment optimization (name the specific drug sequence and dates)
- Z88x codes → drug allergies (list specific allergens)
- Z66/Z515 → DNR/palliative care goals
- Weight loss ≥10% → cachexia or disease progression (cite baseline and nadir weights)
- Readmission <30 days → unstable underlying condition
- Tacrolimus/Mycophenolate/steroids → transplant recipient (infection/rejection risk)
- eMAR "Not Given" for critical drugs → adherence risk
- Blood products + vasopressors + ventilation → critical illness severity
- Time from last discharge to death <30 days → rapid terminal decline
- Insurance transition Private→Medicare → crossed age 65
- Discharge destinations HOME → HOME HEALTH → SNF → LTACH → death = functional decline trajectory
Evidence anchors are required: Every insight must cite specific ICD codes, exact dates, drug names with doses, organism names, DRG severity/mortality scores, or numeric values. Vague summaries without supporting data are not acceptable.
End the analysis with FINISH: followed by the full structured report.
Query Efficiency Rules
These rules prevent the most common analysis failures — wasting queries on granular data instead of building clinical synthesis.
One query per table, then synthesize. Query each table with a consolidated SELECT that gets all needed columns at once. Use OFFSET to paginate, never reissue the same query with different columns.
OMR: one query, one trend summary. Never issue multiple OMR queries to step through measurements chronologically. One query → extract baseline, min, max, final values → report trend and percent changes.
Microbiology: one query, separate positives from negatives. Never issue per-admission microbiology queries. One query → report positive cultures with organisms and sensitivities → summarize negative cultures as a count.
ICU events: paginate, don't re-query. For icu_inputevents, use LIMIT 50 then OFFSET 50 to get blood products/nutrition that appear later. Never re-query the same stay_id with different filters.
Diagnoses: use consolidated queries. For multi-admission patients, use subject_id-level queries with GROUP BY for recurring diagnoses. Don't query each hadm_id individually unless you need specific seq_num detail for a particular admission.
Skip ICU event tables when icu_icustays returns empty. For non-ICU patients, use saved time for deeper eMAR and POE analysis.
For >3 ICU stays, focus on the top-severity stays (longest LOS or highest DRG severity), not all stays exhaustively.
If a query fails with a column error, correct it using the pitfalls table — do not call describe_table.
1---2name: mimic-patient-analysis3description: Comprehensive patient analysis using the MIMIC-IV clinical database. Use this skill whenever asked to analyze, summarize, or investigate a patient's medical history, hospital admissions, diagnoses, medications, procedures, or clinical course from a MIMIC-IV SQLite database. Triggers on prompts like "Analyze patient [ID]", "summarize patient history", "what happened to patient X", or any request to explore patient-level EHR data from MIMIC-IV tables.4---56# MIMIC-IV Patient Analysis78Perform a comprehensive, systematic analysis of a patient's complete clinical record from the MIMIC-IV database by querying the SQLite database directly and efficiently.910## Database Structure1112The database has 27 tables. Key tables and their primary columns:1314**Core patient tables:**15- `hosp_patients` — demographics: `subject_id, gender, anchor_age, anchor_year, anchor_year_group, dod`16- `hosp_admissions` — hospital stays: `subject_id, hadm_id, admittime, dischtime, deathtime, admission_type, admission_location, discharge_location, insurance, language, marital_status, race, edregtime, edouttime, hospital_expire_flag`1718**Clinical data (per admission):**19- `hosp_diagnoses_icd` — ICD diagnoses: `subject_id, hadm_id, seq_num, icd_code, icd_version`20- `hosp_d_icd_diagnoses` — diagnosis dictionary: `icd_code, icd_version, long_title`21- `hosp_procedures_icd` — ICD procedures: `subject_id, hadm_id, seq_num, chartdate, icd_code, icd_version`22- `hosp_d_icd_procedures` — procedure dictionary: `icd_code, icd_version, long_title`23- `hosp_drgcodes` — DRG billing: `subject_id, hadm_id, drg_type, drg_code, description, drg_severity, drg_mortality`24- `hosp_services` — clinical service: `subject_id, hadm_id, transfertime, prev_service, curr_service`25- `hosp_transfers` — unit movements: `subject_id, hadm_id, transfer_id, eventtype, careunit, intime, outtime`2627**Medications:**28- `hosp_prescriptions` — prescribed drugs: `subject_id, hadm_id, starttime, stoptime, drug, drug_type, dose_val_rx, dose_unit_rx, route`29- `hosp_emar` — administration record: `subject_id, hadm_id, emar_id, charttime, medication, event_txt, scheduletime`30- `hosp_pharmacy` — pharmacy fills: `subject_id, hadm_id, pharmacy_id, medication, starttime, stoptime` (**column is `medication`, NOT `drug`**)3132**Diagnostics:**33- `hosp_microbiologyevents` — cultures: `subject_id, hadm_id, charttime, spec_type_desc, test_name, org_name, interpretation, comments`34- `hosp_omr` — vitals/anthropometrics: `subject_id, chartdate, seq_num, result_name, result_value`35- `hosp_hcpcsevents` — billing codes: `subject_id, hadm_id, chartdate, hcpcs_cd, short_description`36- `hosp_d_hcpcs` — HCPCS dictionary: `code, category, long_description, short_description`3738**Orders:**39- `hosp_poe` — provider orders: `subject_id, hadm_id, poe_id, ordertime, order_type, order_subtype, transaction_type, order_status`4041**ICU tables (only present if patient had ICU stay):**42- `icu_icustays` — ICU episodes: `subject_id, hadm_id, stay_id, first_careunit, last_careunit, intime, outtime, los`43- `icu_inputevents` — IV fluids/medications: `stay_id, starttime, endtime, itemid, amount, amountuom, ordercategoryname`44- `icu_outputevents` — urine/drainage: `stay_id, charttime, itemid, value, valueuom`45- `icu_procedureevents` — ICU procedures: `stay_id, starttime, endtime, itemid, value, valueuom, ordercategoryname`46- `icu_d_items` — ICU item dictionary: `itemid, label, category`4748## Critical Column Name Pitfalls4950| Table | WRONG | CORRECT |51|-------|-------|---------|52| `hosp_transfers` | `transfertime` | `intime` |53| `hosp_poe` | `order_time` | `ordertime` |54| `hosp_omr` | `charttime` | `chartdate` |55| `hosp_pharmacy` | `drug` | `medication` |56| `hosp_hcpcsevents` JOIN `hosp_d_hcpcs` | `ON h.hcpcs_cd = d.hcpcs_cd` | `ON h.hcpcs_cd = d.code` |5758## Analysis Workflow5960Start with `get_database_info` to confirm table availability, then query directly — **do not call `describe_table` before each query**; use the column names listed above.6162### Step 1 — Patient demographics63```sql64SELECT * FROM hosp_patients WHERE subject_id = <patient_id>65```66`anchor_age` is age in `anchor_year` (dates are shifted for privacy). If `dod` is not null, the patient died.6768### Step 2 — All hospital admissions69```sql70SELECT * FROM hosp_admissions WHERE subject_id = <patient_id> ORDER BY admittime71```72For each `hadm_id`, note: admission/discharge times, type, source, destination, insurance, hospital_expire_flag.7374**If `dod` is not null**, compute time from last discharge to death:75- `last_dischtime` to `dod` in days = how long the patient survived after final hospitalization.7677**For multi-admission patients**, compute readmission intervals:78```sql79SELECT hadm_id, admittime, dischtime,80 CAST((julianday(admittime) - julianday(LAG(dischtime) OVER (ORDER BY admittime))) AS INTEGER) AS days_since_last_discharge81FROM hosp_admissions WHERE subject_id = <patient_id>82ORDER BY admittime83```84Track discharge destination progression (HOME → HOME HEALTH → SNF → LTACH → hospital death) as a functional decline signal.8586### Step 3 — ICU stays87```sql88SELECT * FROM icu_icustays WHERE subject_id = <patient_id> ORDER BY intime89```90Empty result = no ICU. If ICU present, note care units and length of stay (`los`).9192#### Step 3.5 — ICU deep dive (when ICU stays exist)9394For patients with **≤3 ICU stays**, query all stay_ids. For **>3 stays**, prioritize by longest `los` and highest severity, covering at least the top 3.9596For each `stay_id`, query in this order:9798```sql99-- 1. Procedures (ventilation, dialysis, invasive lines)100SELECT pe.starttime, pe.endtime, d.label, d.category, pe.value, pe.valueuom101FROM icu_procedureevents pe102JOIN icu_d_items d ON pe.itemid = d.itemid103WHERE pe.stay_id = <stay_id>104ORDER BY pe.starttime105106-- 2. Inputs (fluids, medications, blood products, nutrition)107SELECT ie.starttime, d.label, d.category, ie.amount, ie.amountuom, ie.ordercategoryname108FROM icu_inputevents ie109JOIN icu_d_items d ON ie.itemid = d.itemid110WHERE ie.stay_id = <stay_id>111ORDER BY ie.starttime LIMIT 50112113-- 3. Outputs (urine, drainage)114SELECT oe.charttime, d.label, oe.value, oe.valueuom115FROM icu_outputevents oe116JOIN icu_d_items d ON oe.itemid = d.itemid117WHERE oe.stay_id = <stay_id>118ORDER BY oe.charttime LIMIT 30119```120121**For each ICU stay, extract and summarize:**122- **Ventilation**: mechanical ventilation duration (label contains "Invasive Ventilation" or "Ventilation")123- **Vasopressors**: Norepinephrine, Epinephrine, Vasopressin, Phenylephrine, Dopamine (total amount)124- **Sedation/analgesia**: Propofol, Fentanyl, Midazolam, Dexmedetomidine125- **Blood products**: Packed RBCs, Platelets, FFP, Cryoprecipitate (`ordercategoryname = 'Blood Products'`)126- **Enteral nutrition**: formula names and total volumes (labels containing "Enteral", "Glucerna", "Promote", "Two Cal")127- **Fluid balance**: sum inputs (mL) minus sum outputs (mL) — positive = net accumulation128- **Vascular access**: arterial line, central line, Foley durations129130Paginate inputs with OFFSET if >50 rows — blood products and nutrition often appear later.131132### Step 4 — Diagnoses (with human-readable names)133```sql134SELECT d.icd_code, d.icd_version, d.long_title, diag.hadm_id, diag.seq_num135FROM hosp_diagnoses_icd diag136JOIN hosp_d_icd_diagnoses d ON diag.icd_code = d.icd_code AND diag.icd_version = d.icd_version137WHERE diag.subject_id = <patient_id>138ORDER BY diag.hadm_id, diag.seq_num139```140`seq_num=1` is the primary diagnosis. Note total diagnosis count per admission (high count = high complexity).141142**For patients with 4+ admissions**, identify recurring diagnoses:143```sql144SELECT d.long_title, COUNT(*) as admission_count145FROM hosp_diagnoses_icd diag146JOIN hosp_d_icd_diagnoses d ON diag.icd_code = d.icd_code AND diag.icd_version = d.icd_version147WHERE diag.subject_id = <patient_id> AND diag.seq_num <= 5148GROUP BY d.long_title149ORDER BY admission_count DESC150LIMIT 20151```152153**Always query for special ICD codes** — clinically critical status flags:154```sql155SELECT d.icd_code, d.long_title, diag.hadm_id156FROM hosp_diagnoses_icd diag157JOIN hosp_d_icd_diagnoses d ON diag.icd_code = d.icd_code AND diag.icd_version = d.icd_version158WHERE diag.subject_id = <patient_id>159 AND (d.icd_code LIKE 'Z88%' -- drug allergies160 OR d.icd_code = 'Z66' -- do not resuscitate161 OR d.icd_code = 'Z515' -- palliative care162 OR d.icd_code LIKE 'Z79%') -- long-term medication use163ORDER BY diag.hadm_id164```165166### Step 5 — Procedures167```sql168SELECT p.hadm_id, p.seq_num, p.chartdate, p.icd_code, proc.long_title169FROM hosp_procedures_icd p170JOIN hosp_d_icd_procedures proc ON p.icd_code = proc.icd_code AND p.icd_version = proc.icd_version171WHERE p.subject_id = <patient_id>172ORDER BY p.hadm_id, p.seq_num173```174175### Step 6 — Medications prescribed176```sql177SELECT drug, COUNT(*) as prescription_count178FROM hosp_prescriptions179WHERE subject_id = <patient_id>180GROUP BY drug181ORDER BY prescription_count DESC182LIMIT 20183```184185Then per-admission detail:186```sql187SELECT hadm_id, drug, starttime, stoptime, dose_val_rx, dose_unit_rx, route188FROM hosp_prescriptions189WHERE subject_id = <patient_id>190ORDER BY hadm_id, starttime191```192193Group medications by clinical class: anticoagulants/antiplatelets, cardiovascular, diuretics, analgesics/opioids, antibiotics, psychiatric agents, immunosuppressants. Note route transitions (IV → PO = improvement; PO/NG = nasogastric feeding from dysphagia). Multiple laxatives (Senna + Bisacodyl + Docusate) indicate immobility or opioid use.194195#### Step 6.5 — Pharmacy fills (cross-validation)196```sql197SELECT hadm_id, medication, starttime, stoptime198FROM hosp_pharmacy199WHERE subject_id = <patient_id>200ORDER BY hadm_id, starttime201LIMIT 50202```203Column is `medication`, not `drug`. Paginate with OFFSET if needed.204205### Step 7 — Physical measurements (BMI, weight, height, BP)206207**Query all OMR data in a single consolidated query:**208```sql209SELECT chartdate, result_name, result_value210FROM hosp_omr211WHERE subject_id = <patient_id>212ORDER BY chartdate213```214215**Synthesize trends, do not report individual data points.** For longitudinal patients with many OMR records, summarize the overall trajectory:216- Weight: report baseline, minimum, maximum, and final values with percent change. Flag ≥5% loss as clinically significant, ≥10% as cachexia risk.217- BP: report overall range (systolic and diastolic) and note any hypertensive spikes (>140/90) or hypotensive episodes (<90/60).218- BMI: report range and trend direction.219- Flag implausible values (e.g., weight of 1731 lbs is likely a typo for 173.1).220221Do NOT issue multiple paginated queries stepping through OMR records month-by-month or year-by-year. One query with a single synthesized trend summary is sufficient and prevents wasting analysis effort on granular data recitation.222223### Step 8 — Microbiology cultures224225**Query all microbiology data in a single consolidated query:**226```sql227SELECT chartdate, spec_type_desc, test_name, org_name, interpretation, comments228FROM hosp_microbiologyevents229WHERE subject_id = <patient_id>230ORDER BY chartdate231```232233Focus on clinically actionable findings:234- **Positive cultures**: Record organism name, specimen type, and antibiotic sensitivity (R/S/I).235- **MRSA colonization**: Note any MRSA screen positive results.236- **Negative cultures**: Summarize as "X cultures negative" rather than listing each one.237- Comments containing "< 10,000 CFU/mL" or "NO GROWTH" = negative.238- Comments containing "MIXED BACTERIAL FLORA" = likely contamination, not a true infection.239240Paginate with OFFSET only if >50 results. Do NOT issue per-admission microbiology queries individually.241242### Step 9 — Clinical service and transfers243```sql244SELECT * FROM hosp_services WHERE subject_id = <patient_id> ORDER BY transfertime245246SELECT hadm_id, eventtype, careunit, intime, outtime247FROM hosp_transfers WHERE subject_id = <patient_id>248ORDER BY hadm_id, intime249LIMIT 50250```251For patients with many admissions, paginate transfers with LIMIT/OFFSET.252253### Step 10 — DRG billing codes254```sql255SELECT * FROM hosp_drgcodes WHERE subject_id = <patient_id>256```257APR-DRG has severity (1-4) and mortality (1-4) scores. Severity 3-4 or mortality 3-4 = high-complexity/high-risk admission.258259### Step 11 — HCPCS events260```sql261SELECT h.hadm_id, h.chartdate, h.hcpcs_cd, d.short_description262FROM hosp_hcpcsevents h263JOIN hosp_d_hcpcs d ON h.hcpcs_cd = d.code264WHERE h.subject_id = <patient_id>265ORDER BY h.chartdate266```267Zero results = no billed procedures (common). G0378 = observation-status admission.268269### Step 12 — eMAR and Provider Orders270```sql271-- Medication administration record272SELECT charttime, medication, event_txt, scheduletime273FROM hosp_emar WHERE subject_id = <patient_id>274ORDER BY charttime LIMIT 50275276-- Provider orders overview277SELECT order_type, COUNT(*) as order_count278FROM hosp_poe WHERE subject_id = <patient_id>279GROUP BY order_type280ORDER BY order_count DESC281282-- Detailed orders for specific admission (when clinically relevant)283SELECT ordertime, order_type, order_subtype, transaction_type, order_status284FROM hosp_poe WHERE subject_id = <patient_id> AND hadm_id = <hadm_id>285ORDER BY ordertime LIMIT 30286```287288The `event_txt` field distinguishes "Administered" from "Not Given" — this reveals medication compliance and route changes. Patterns of held critical drugs (immunosuppressants, anticoagulants) are adherence risk signals. "Not Given per Sliding Scale" for insulin is expected, not a compliance issue.289290Order type distribution (Medications / Lab / Radiology / Nutrition / General Care) characterizes admission intensity. Rehabilitation consults (Speech/Swallowing, Occupational Therapy, Physical Therapy) signal functional impairment workup.291292## Producing the Final Report293294After gathering data, produce a **structured report with markdown headers and tables**. The report must be comprehensive enough to answer detailed clinical questions about any aspect of the patient's care.295296### Required report sections:2972981. **Demographics** — age, sex, race, insurance, vital status (alive/deceased + date); if deceased, compute days from last discharge to death; note insurance transitions (Private→Medicare = age 65)2993002. **Admission Summary** — number of admissions, date range, types/sources; present as a **markdown table** for multi-admission patients with columns: hadm_id, admit date, discharge date, type, primary diagnosis, discharge location, LOS; computed readmission intervals highlighting any <30-day readmissions3013023. **ICU Course** — whether ICU was needed, which units, LOS per stay; key interventions: ventilation duration, vasopressors (drug names + amounts), sedation agents, blood products (type + volume), enteral nutrition, fluid balance (total inputs − outputs mL)3033044. **Primary Diagnoses by Admission** — primary condition per hadm_id; total diagnosis count per admission to indicate complexity; identify highest-complexity admissions3053065. **Comorbidities** — significant secondary diagnoses; for multi-admission patients, note recurring conditions with frequency counts3073086. **Procedures** — surgical and therapeutic interventions with dates; link to relevant admissions3093107. **Medications** — organized by clinical class (anticoagulants, cardiovascular, diuretics, analgesics, antibiotics, immunosuppressants); for multi-admission patients, top prescriptions by frequency; route transitions and polypharmacy patterns; specific dose information for key drugs3113128. **Diagnostics** — positive culture results (organism + specimen + sensitivity pattern); MRSA colonization status; physical measurement trends (summarized as ranges and percent changes, NOT individual data points); negative culture summary3133149. **Clinical Service Trajectory** — services and care unit progression; transition patterns (e.g., Trauma SICU → Neuro SICU → Med/Surg = recovery)31531610. **Key Clinical Insights** — clinically meaningful patterns with specific evidence:317 - Discharge to rehab/SNF → functional impairment318 - Multiple laxatives (Senna + Bisacodyl + Docusate) → immobility or opioid use319 - PO/NG drug routes → nasogastric feeding (dysphagia)320 - Sequential anticoagulant changes → treatment optimization (name the specific drug sequence and dates)321 - Z88x codes → drug allergies (list specific allergens)322 - Z66/Z515 → DNR/palliative care goals323 - Weight loss ≥10% → cachexia or disease progression (cite baseline and nadir weights)324 - Readmission <30 days → unstable underlying condition325 - Tacrolimus/Mycophenolate/steroids → transplant recipient (infection/rejection risk)326 - eMAR "Not Given" for critical drugs → adherence risk327 - Blood products + vasopressors + ventilation → critical illness severity328 - Time from last discharge to death <30 days → rapid terminal decline329 - Insurance transition Private→Medicare → crossed age 65330 - Discharge destinations HOME → HOME HEALTH → SNF → LTACH → death = functional decline trajectory331332**Evidence anchors are required**: Every insight must cite specific ICD codes, exact dates, drug names with doses, organism names, DRG severity/mortality scores, or numeric values. Vague summaries without supporting data are not acceptable.333334End the analysis with `FINISH:` followed by the full structured report.335336## Query Efficiency Rules337338These rules prevent the most common analysis failures — wasting queries on granular data instead of building clinical synthesis.3393401. **One query per table, then synthesize.** Query each table with a consolidated SELECT that gets all needed columns at once. Use OFFSET to paginate, never reissue the same query with different columns.3413422. **OMR: one query, one trend summary.** Never issue multiple OMR queries to step through measurements chronologically. One query → extract baseline, min, max, final values → report trend and percent changes.3433443. **Microbiology: one query, separate positives from negatives.** Never issue per-admission microbiology queries. One query → report positive cultures with organisms and sensitivities → summarize negative cultures as a count.3453464. **ICU events: paginate, don't re-query.** For `icu_inputevents`, use LIMIT 50 then OFFSET 50 to get blood products/nutrition that appear later. Never re-query the same stay_id with different filters.3473485. **Diagnoses: use consolidated queries.** For multi-admission patients, use subject_id-level queries with GROUP BY for recurring diagnoses. Don't query each hadm_id individually unless you need specific seq_num detail for a particular admission.3493506. **Skip ICU event tables when `icu_icustays` returns empty.** For non-ICU patients, use saved time for deeper eMAR and POE analysis.3513527. **For >3 ICU stays**, focus on the top-severity stays (longest LOS or highest DRG severity), not all stays exhaustively.3533548. **If a query fails with a column error**, correct it using the pitfalls table — do not call `describe_table`.