|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Window Functions |
| 4 | +parent: SQL |
| 5 | +nav_order: 11 |
| 6 | +--- |
| 7 | + |
| 8 | +# Window Functions |
| 9 | + |
| 10 | +A window function performs a calculation across a frame of data rows around the current row and finds a result for each row. |
| 11 | + |
| 12 | +`PARTITION` and `ORDER` define the frame of data over which the calculations are made. |
| 13 | + |
| 14 | +You can use window functions in following three categories: |
| 15 | + |
| 16 | +1. Aggregate Functions: `COUNT()`, `MIN()`, `MAX()`, `AVG()`, and `SUM()`. |
| 17 | +2. Ranking Functions: `ROW_NUMBER()`, `RANK()`, `DENSE_RANK()`, `PERCENT_RANK()`, and `NTILE()`. |
| 18 | +3. Analytic Functions: `CUME_DIST()`, `LAG()`, and `LEAD()`. |
| 19 | + |
| 20 | +The syntax of a window function is as follows: |
| 21 | + |
| 22 | +```sql |
| 23 | +function_name (expression [, expression...]) |
| 24 | + OVER ( |
| 25 | + PARTITION BY expression [, expression...] |
| 26 | + ORDER BY expression [ASC | DESC] [, ...] |
| 27 | + ) |
| 28 | +``` |
| 29 | + |
| 30 | +The `PARTITION BY` and `ORDER BY` clauses are optional. |
| 31 | +{.. note} |
| 32 | + |
| 33 | + |
| 34 | +## Ranking Functions |
| 35 | + |
| 36 | +Ranking functions assign an incremental rank to each row in the frame. |
| 37 | +The increase in rank number is based on the ranking function implementation, though the rank is mostly determined by field values in the `ORDER BY` list. If the `PARTITION BY` clause is present, the state of ranking functions (incremental rank number maintained) is reset. |
| 38 | + |
| 39 | +If the ranking functions is not used with the window definition that defines the order of data rows, the result is undetermined. |
| 40 | + |
| 41 | +In this case, `ROW_NUMBER` assigns row number to data rows in random order. `RANK` and `DENSE_RANK` always assign a rank of 1 to each row. |
| 42 | + |
| 43 | +### ROW_NUMBER |
| 44 | + |
| 45 | +The row number function assigns a row number to each row. As a special case, the row number is always increased by 1 regardless of the fields specified in the `ORDER BY` list. |
| 46 | + |
| 47 | +```sql |
| 48 | +od> SELECT gender, balance, ROW_NUMBER() OVER(PARTITION BY gender ORDER BY balance) AS num FROM accounts; |
| 49 | +``` |
| 50 | + |
| 51 | +| gender | balance | num |
| 52 | +:--- | :--- |
| 53 | +| F | 32838 | 1 |
| 54 | +| M | 4180 | 1 |
| 55 | +| M | 5686 | 2 |
| 56 | +| M | 39225 | 3 |
| 57 | + |
| 58 | +### RANK |
| 59 | + |
| 60 | +The `RANK` function assigns a rank to each row. For rows that have the same values for fields specified in the `ORDER BY` list, the same rank is assigned. If this is the case, the next few ranks is skipped depending on the number of ties. |
| 61 | + |
| 62 | +```sql |
| 63 | +od> SELECT gender, RANK() OVER(ORDER BY gender DESC) AS rnk FROM accounts; |
| 64 | +``` |
| 65 | + |
| 66 | +| gender | rank |
| 67 | +:--- | :--- |
| 68 | +| M | 1 |
| 69 | +| M | 1 |
| 70 | +| M | 1 |
| 71 | +| F | 4 |
| 72 | + |
| 73 | +### DENSE_RANK |
| 74 | + |
| 75 | +Similar to the `RANK` function, `DENSE_RANK` also assigns a rank to each row. The difference is there is no gap between the ranks. |
| 76 | + |
| 77 | +```sql |
| 78 | +SELECT gender, DENSE_RANK() OVER(ORDER BY gender DESC) AS rnk FROM accounts; |
| 79 | +``` |
| 80 | + |
| 81 | +| gender | rank |
| 82 | +:--- | :--- |
| 83 | +| M | 1 |
| 84 | +| M | 1 |
| 85 | +| M | 1 |
| 86 | +| F | 2 |
0 commit comments