Chapter 7 · CTEs
20. Multi-CTE Financial Health Report
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.
Tables
- PKidINT
- first_nameTEXT
- last_nameTEXT
- emailTEXT
- phoneTEXT
- date_of_birthTEXT
- cityTEXT
- stateTEXT
- countryTEXT
- created_atTIMESTAMP
- PKidINT
- FKcustomer_id→ customers.idINT
- account_typeTEXT
- balanceREAL
- currencyTEXT
- opened_atTEXT
- statusTEXT
- PKidINT
- FKcustomer_id→ customers.idINT
- loan_typeTEXT
- principalREAL
- interest_rateREAL
- term_monthsINT
- start_dateTEXT
- statusTEXT
- PKidINT
- FKloan_id→ loans.idINT
- payment_dateTEXT
- amountREAL
- principal_paidREAL
- interest_paidREAL
- remaining_balanceREAL
Need help?
Ask Sol
AI tutor — looks at your current query and nudges you, without spoiling the answer.
Solution
Try the hints first — solving it yourself is what makes it stick.