Build a comprehensive patient journey report using CTEs. For each patient, show: total visits, total prescriptions, total lab tests, total billed amount, and days between their first and last visit (patient tenure). Return patient full name, total_visits, total_prescriptions, total_lab_tests, total_billed, and tenure_days. Sort by tenure_days descending.
Schema
🔑idINT
first_nameTEXT
last_nameTEXT
date_of_birthTEXT
genderTEXT
emailTEXT
phoneTEXTNULL
blood_typeTEXT
cityTEXT
stateTEXT
🔗insurance_idINTNULL
created_atTIMESTAMP
postgresql
Run your query to see results
Press Ctrl + Enter to execute