Calculate a diversification score for each customer based on how many distinct financial products they hold. Products include: distinct account types, distinct card types, and distinct loan types. Return first_name, last_name, num_account_types, num_card_types, num_loan_types, and total_products (the sum of all three). Use CTEs for each product category. Include all customers (use 0 for missing products). Order by total_products descending, last_name 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