Others

How to Calculate a Rolling Percent Rank in SQL Server

Huma Ashraf ChHuma Ashraf Ch Nov 3, 2026 869 views

Question details

The user needs to calculate a percent rank over a rolling window of rows (such as the current record and preceding records) in SQL Server.

How to Calculate a Rolling Percent Rank in SQL Server
Product
SQL Server
Device & OS
not provided
Scenario
Performing time-series or sequential data analysis where percentiles must be calculated dynamically over a specific moving window of rows.
Observed behavior
SQL Server's native PERCENT_RANK() function does not support the ROWS BETWEEN window frame syntax, requiring an alternative query pattern to achieve the rolling calculation.
Before you start

Ensure your database tables are properly indexed on the columns used for ordering and partitioning (such as date or symbol columns) to maintain optimal query performance.

Solution 1Recommended

Calculate Rolling Percent Rank using CROSS APPLY

Use the CROSS APPLY operator to isolate the rolling subset of rows for each record and calculate the rank manually.

Since the native PERCENT_RANK() function cannot be restricted by a ROWS BETWEEN clause, the most efficient workaround is using CROSS APPLY. This method extracts the rolling window for each row, allowing you to manually compute the percentile using ROW_NUMBER() and COUNT().

1
Identify ordering columns

Determine the columns that define your sequence and partition, such as a Date or ID column, and a Category or Symbol column.

2
Write the CROSS APPLY statement

Construct a CROSS APPLY block that selects the current row and the specific number of preceding rows (e.g., using TOP 4 ORDER BY Date DESC) to form the rolling window.

3
Calculate row number and count

Inside the APPLY block, use ROW_NUMBER() to determine the rank of the current value among the selected window, and COUNT(*) to get the total number of rows in that window.

4
Compute the percentile

Apply the standard percent rank formula: divide (Rank - 1) by (Total Count - 1) in your outer SELECT statement to yield the rolling percent rank.

Calculate Rolling Percent Rank using CROSS APPLY
Execution Plan Review: For large tables, it is highly recommended to review the execution plan. Applying an index on the SYMBOL and QUOTE_DATE columns will significantly reduce the cost of the CROSS APPLY operation.
Analyze Data in WPS Spreadsheet

Calculate Rolling Percent Ranks Easily in WPS Office

If writing complex SQL queries for rolling calculations is too resource-intensive, you can export your SQL Server data to WPS Spreadsheet and calculate rolling percent ranks in seconds using built-in functions.

  1. 1. Export your SQL data: Export your target dataset from SQL Server Management Studio as a CSV or Excel file and open it in WPS Spreadsheet.
  2. 2. Select the target cell: Click on an empty cell in a new column adjacent to the last data point of your first rolling window.
  3. 3. Enter the PERCENTRANK formula: Type the formula =PERCENTRANK.INC(A2:A5, A5), assuming A2:A5 represents your rolling window of 4 rows and A5 is the current value to evaluate.
  4. 4. Apply to the dataset: Drag the fill handle down the column to automatically calculate the rolling percent rank for the rest of your exported data.
Familiar spreadsheet interface for quick, visual data analysisBuilt-in PERCENTRANK.INC function for effortless percentile calculationsSeamlessly compatible with Microsoft Excel file formats (.xlsx)Handles large exported SQL datasets smoothly and without lag
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't PERCENT_RANK() work with ROWS BETWEEN in SQL Server?

In SQL Server, the implementation of the PERCENT_RANK() window function applies to the entire partition. It currently does not support the window frame clause (ROWS or RANGE BETWEEN), meaning you cannot restrict its calculation to a specific moving subset of rows.

Can I use ROW_NUMBER() to manually calculate a percent rank?

Yes. By isolating a subset of data (like a rolling window), calculating the ROW_NUMBER() of the target value, and dividing it by the COUNT() of the subset based on the formula (Rank - 1) / (Count - 1), you can manually reproduce percent rank logic.

Does the CROSS APPLY method impact SQL Server performance?

CROSS APPLY can be resource-intensive on very large tables because it evaluates the subquery logic for every single row in the outer query. To optimize performance, ensure that the columns used to define the rolling window are properly indexed.