single
Advanced Window Functions: NTILE, PERCENT_RANK, CUME_DIST
Scorri per mostrare il menu
In advanced analytics, window functions allow you to perform complex calculations across sets of rows related to your current row. Beyond ranking and running totals, SQL provides powerful window functions specifically designed for quantile and percentile analysis: NTILE, PERCENT_RANK, and CUME_DIST.
The NTILE function divides your result set into a specified number of roughly equal groups, assigning a group number to each row. For example, using NTILE(4) on a sorted list will assign each row to a quartile (1 to 4), which is useful for segmenting customers, employees, or products into performance buckets.
PERCENT_RANK computes the relative rank of a row within a partition as a percentage between 0 and 1, showing how a value compares to others in its group. CUME_DIST calculates the cumulative distribution of a value—essentially, the proportion of rows with values less than or equal to the current row.
A quantile divides a dataset into equal-sized, ordered groups. Common quantiles include quartiles (4 groups), quintiles (5), and deciles (10). A percentile indicates the percentage of data points below a particular value. In business analytics, quantiles and percentiles help identify top and bottom performers, target segments, or outliers for focused strategies.
12345678910111213SELECT c.customer_id, c.first_name, c.last_name, SUM(o.total) AS lifetime_value, NTILE(4) OVER (ORDER BY SUM(o.total) DESC) AS revenue_quartile FROM customers c JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.first_name, c.last_name ORDER BY revenue_quartile, lifetime_value DESC;
Imagine a scenario where the marketing team wants to target the top 10% of customers by lifetime value. You can use PERCENT_RANK to identify which customers fall into this elite decile.
12345678910111213141516171819SELECT customer_id, first_name, last_name, lifetime_value, pct_rank FROM ( SELECT c.customer_id, c.first_name, c.last_name, SUM(o.total) AS lifetime_value, PERCENT_RANK() OVER (ORDER BY SUM(o.total) DESC) AS pct_rank FROM customers c JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.first_name, c.last_name ) ranked WHERE pct_rank <= 0.1 ORDER BY pct_rank;
Scorri per iniziare a programmare
Assign each employee to a performance quartile within their department using window functions.
- Use the
NTILEwindow function to divide employees into four quartiles based on their performance score within each department. - Partition the ranking by
department_id. - Order each partition by
scorein descending order. - Return each employee's ID, first and last name, department ID and name, performance score, and their assigned quartile.
Soluzione
Grazie per i tuoi commenti!
single
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione