Course contentsShow
← Databases & SQL
Lesson 292 of 1,9534. Advanced Data AggregationFree lesson

Cumulative Distribution Metrics

Calculate rank-based distributions for statistical analysis of data spread.

In this lesson, you will learn how to determine the relative standing of specific data points within your dataset using cumulative distribution metrics.

What is a Cumulative Distribution?

While a simple rank tells you if a value is "1st" or "5th," a cumulative distribution tells you what percentage of your data falls at or below a specific value. Think of it as a way to visualize how your data is "spread" across the board. If you have a test score and your cumulative distribution value is 0.85, it means your score is equal to or better than 85% of all test-takers.

Implementing with CUME_DIST()

In SQL, we use the CUME_DIST() window function to calculate this metric. It calculates the relative position of a row by taking the number of rows with values less than or equal to the current row, and dividing that by the total number of rows in the partition.

Imagine we have a table of sales_performance and want to see how each salesperson compares to the rest of the team:

SELECT 
    salesperson_name,
    total_sales,
    CUME_DIST() OVER (ORDER BY total_sales) AS percentile_rank
FROM sales_performance;

How the Math Works

If you have 10 employees, and an employee’s total_sales is the 8th highest value, the window function doesn't just assign a rank of 8. Instead, it looks at the percentage. If there are no ties, CUME_DIST() will return 0.8 (representing the 80th percentile).

Crucially, if there are ties (two employees with the same sales), CUME_DIST() groups those "peers" together. All tied values receive the same cumulative distribution result, which represents the position of the last member in that tied group. This helps you understand the "density" of your data—knowing if many people are clumped together at the same performance level or if the distribution is widely spread out.

By using this metric, you move beyond simple sorting and begin to analyze the statistical shape of your data, allowing you to identify outliers or clusters within your analytical frames.

Key Takeaway: Cumulative distribution metrics quantify the relative position of a value as a percentage of the total set, allowing you to measure the density and statistical standing of data points within your windows.