Welcome Back

Google icon Sign in with Google
OR
I agree to abide by Pharmadaily Terms of Service and its Privacy Policy

Create Account

Google icon Sign up with Google
OR
By signing up, you agree to our Terms of Service and Privacy Policy
Instagram
youtube
Facebook

Use NTILE(4) to Divide Customers into Revenue Quartiles

Use NTILE(4) to Divide Customers into Revenue Quartiles

Customers Table:



Payments Table:


Query Explanation:

  • The inner query calculates TotalRevenue per customer by summing their payments.

  • The outer query uses NTILE(4) to assign each customer to one of four quartiles:

    • Quartile 1: Highest spenders

    • Quartile 4: Lowest spenders

  • ORDER BY TotalRevenue DESC: ensures that higher revenue customers fall into the first quartile.


SQL Query:

SELECT 
    CustomerID,
    TotalRevenue,
    NTILE(4) OVER (ORDER BY TotalRevenue DESC) AS RevenueQuartile
FROM (
    SELECT 
        CustomerID,
        SUM(Amount) AS TotalRevenue
    FROM 
        Payments
    GROUP BY 
        CustomerID
) AS CustomerRevenue;


Output: