Marketing wants to segment customers into four equal-sized tiers by lifetime spend, to target the top tier with a loyalty offer. Compute each customer's total spend, then use NTILE(4) to assign a quartile from 1 (highest spenders) to 4 (lowest).
Return CustomerId, FirstName, LastName, TotalSpent, and SpendQuartile, ordered by TotalSpent descending (ties broken by CustomerId ascending).
| Column | Type |
|---|---|
| CustomerId | INTEGER (Primary Key) |
| FirstName | TEXT |
| LastName | TEXT |
| Column | Type |
|---|---|
| InvoiceId | INTEGER (Primary Key) |
| CustomerId | INTEGER (Foreign Key -> Customer.CustomerId) |
| InvoiceDate | TIMESTAMP |
| Total | NUMERIC(10,2) |
Ready to take your skills to the next level? Enroll in our comprehensive Data Analyst Career Track to master SQL, Python, Excel, and Power BI.
Run your code to see the result here.