Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Advanced Window Functions: NTILE, PERCENT_RANK, CUME_DIST | Analytical SQL Techniques
SQL for Analytical Reports
セクション 1.  13
single

single

bookAdvanced Window Functions: NTILE, PERCENT_RANK, CUME_DIST

メニューを表示するにはスワイプしてください

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
タスク

スワイプしてコーディングを開始

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.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  13
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt