Skip to main content
All Problems
hard0% acceptance

Customer Spending by Category (CTE with Percentages)

Build a spending breakdown by transaction type for each customer. CTE 1: customer_totals — total transaction amount per customer. CTE 2: type_totals — total amount per customer per transaction type. Main query: join both to compute the percentage of spend in each type. Return first_name, last_name, type, type_amount, total_amount, and spend_pct rounded to 1 decimal. Sort by last_name, spend_pct descending. Limit to 50.

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