Build a comprehensive financial health report per customer using multiple CTEs. The report should include: full_name, total_balance (from accounts), total_outstanding_loans (sum of remaining_balance from the latest payment per active loan), debt_to_asset_ratio (total_outstanding_loans / total_balance, rounded to 4, NULL if no balance), and health_status classified as 'Excellent' if ratio < 0.3, 'Good' if < 0.6, 'At Risk' if < 1.0, else 'Critical'. Only include customers with both accounts and active loans. Order by debt_to_asset_ratio ascending.
Schema
🔑idINT
first_nameTEXT
last_nameTEXT
emailTEXT
phoneTEXTNULL
date_of_birthTEXT
cityTEXT
stateTEXT
countryTEXT
created_atTIMESTAMP
postgresql
Run your query to see results
Press Ctrl + Enter to execute