How to Calculate a Rolling Percent Rank in SQL Server
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.

- 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.
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.
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().
Determine the columns that define your sequence and partition, such as a Date or ID column, and a Category or Symbol column.
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.
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.
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 using a Correlated Subquery
Use a correlated subquery in the SELECT clause to count the strictly lesser values within your designated rolling window.
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. 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. 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. 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. 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.

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.




