2024-08-05 03:19:21 +00:00
|
|
|
---
|
|
|
|
slug: /en/sql-reference/window-functions/percent_rank
|
|
|
|
sidebar_label: percent_rank
|
|
|
|
sidebar_position: 8
|
|
|
|
---
|
|
|
|
|
|
|
|
# percent_rank
|
|
|
|
|
|
|
|
returns the relative rank (i.e. percentile) of rows within a window partition.
|
|
|
|
|
|
|
|
**Syntax**
|
|
|
|
|
|
|
|
Alias: `percentRank` (case-sensitive)
|
|
|
|
|
|
|
|
```sql
|
2024-11-25 18:52:59 +00:00
|
|
|
percent_rank ()
|
|
|
|
OVER ([[PARTITION BY grouping_column] [ORDER BY sorting_column]
|
2024-08-06 10:07:42 +00:00
|
|
|
[RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING]] | [window_name])
|
2024-08-05 03:19:21 +00:00
|
|
|
FROM table_name
|
2024-08-06 10:07:42 +00:00
|
|
|
WINDOW window_name as ([PARTITION BY grouping_column] [ORDER BY sorting_column] RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
|
2024-08-05 03:19:21 +00:00
|
|
|
```
|
|
|
|
|
2024-08-06 10:07:42 +00:00
|
|
|
The default and required window frame definition is `RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING`.
|
2024-08-05 03:19:21 +00:00
|
|
|
|
|
|
|
For more detail on window function syntax see: [Window Functions - Syntax](./index.md/#syntax).
|
|
|
|
|
|
|
|
**Example**
|
|
|
|
|
|
|
|
|
|
|
|
Query:
|
|
|
|
|
|
|
|
```sql
|
|
|
|
CREATE TABLE salaries
|
|
|
|
(
|
|
|
|
`team` String,
|
|
|
|
`player` String,
|
|
|
|
`salary` UInt32,
|
|
|
|
`position` String
|
|
|
|
)
|
|
|
|
Engine = Memory;
|
|
|
|
|
|
|
|
INSERT INTO salaries FORMAT Values
|
|
|
|
('Port Elizabeth Barbarians', 'Gary Chen', 195000, 'F'),
|
|
|
|
('New Coreystad Archdukes', 'Charles Juarez', 190000, 'F'),
|
|
|
|
('Port Elizabeth Barbarians', 'Michael Stanley', 150000, 'D'),
|
|
|
|
('New Coreystad Archdukes', 'Scott Harrison', 150000, 'D'),
|
|
|
|
('Port Elizabeth Barbarians', 'Robert George', 195000, 'M'),
|
|
|
|
('South Hampton Seagulls', 'Douglas Benson', 150000, 'M'),
|
|
|
|
('South Hampton Seagulls', 'James Henderson', 140000, 'M');
|
|
|
|
```
|
|
|
|
|
|
|
|
```sql
|
2024-11-25 18:52:59 +00:00
|
|
|
SELECT player, salary,
|
2024-08-05 03:19:21 +00:00
|
|
|
percent_rank() OVER (ORDER BY salary DESC) AS percent_rank
|
|
|
|
FROM salaries;
|
|
|
|
```
|
|
|
|
|
|
|
|
Result:
|
|
|
|
|
|
|
|
```response
|
|
|
|
|
|
|
|
┌─player──────────┬─salary─┬───────percent_rank─┐
|
|
|
|
1. │ Gary Chen │ 195000 │ 0 │
|
|
|
|
2. │ Robert George │ 195000 │ 0 │
|
|
|
|
3. │ Charles Juarez │ 190000 │ 0.3333333333333333 │
|
|
|
|
4. │ Michael Stanley │ 150000 │ 0.5 │
|
|
|
|
5. │ Scott Harrison │ 150000 │ 0.5 │
|
|
|
|
6. │ Douglas Benson │ 150000 │ 0.5 │
|
|
|
|
7. │ James Henderson │ 140000 │ 1 │
|
|
|
|
└─────────────────┴────────┴────────────────────┘
|
|
|
|
|
|
|
|
```
|