Segment customers into quartiles by lifetime value and compute days since their most recent purchase.
| Column | Type |
|---|---|
| CustomerId | INTEGER (PK) |
| FirstName | TEXT NOT NULL |
| LastName | TEXT NOT NULL |
| Company | TEXT |
| Country | TEXT |
| TEXT NOT NULL | |
| SupportRepId | INTEGER (FK → Employee) |
| Column | Type |
|---|---|
| InvoiceId | INTEGER (PK) |
| CustomerId | INTEGER (FK) |
| InvoiceDate | TIMESTAMP NOT NULL |
| BillingCountry | TEXT |
| Total | NUMERIC(10,2) NOT NULL |
LTV_Quartile via NTILE(4) OVER (ORDER BY LTV DESC) (Q1 = highest LTV)DaysSinceLastPurchase from '2014-01-01'::date as the reference "today"FullName, LTV, LTV_Quartile (as 'Q1'..'Q4'), LastPurchase (date), DaysSinceLastPurchaseReady 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.