Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Advanced Window Functions: NTILE, PERCENT_RANK, CUME_DIST | Analytical SQL Techniques
SQL for Analytical Reports
Sectie 1. Hoofdstuk 13
single

single

bookAdvanced Window Functions: NTILE, PERCENT_RANK, CUME_DIST

Veeg om het menu te tonen

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.

Note
Definition

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.

12345678910111213
SELECT 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;
copy

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.

12345678910111213141516171819
SELECT 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;
copy
Taak

Veeg om te beginnen met coderen

Assign each employee to a performance quartile within their department using window functions.

  • Use the NTILE window 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 score in descending order.
  • Return each employee's ID, first and last name, department ID and name, performance score, and their assigned quartile.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 13
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

some-alt