|
| 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 BY` and `ORDER BY` define the frame of data over which the calculation is made. |
| 13 | + |
| 14 | +You can use window functions in the 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 | +To use window functions, enable the new SQL engine: |
| 34 | + |
| 35 | +```json |
| 36 | +PUT _cluster/settings |
| 37 | +{ |
| 38 | + "persistent": { |
| 39 | + "opendistro.sql.engine.new.enabled" : "true" |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Ranking functions |
| 45 | + |
| 46 | +Ranking functions assign an incremental ranking value to each row in the frame. |
| 47 | + |
| 48 | +The increase in the ranking value depends on how the ranking function is implemented. The ranking value is mostly determined by the field values in the `ORDER BY` clause. If the `PARTITION BY` clause is also present, the ranking function resets its state, while maintaining the incremental ranking value. |
| 49 | + |
| 50 | +If you use the ranking function without the `ORDER BY` clause, the result is undetermined. Without the `ORDER BY` clause, `ROW_NUMBER` assigns a random number to each data row while `RANK` and `DENSE_RANK` assign a ranking value of 1 to each data row. |
| 51 | + |
| 52 | +### RANK |
| 53 | + |
| 54 | +The `RANK` function assigns a ranking value to each row of a result set. |
| 55 | +It assigns the same ranking value for the same field values specified in the `ORDER BY` list. |
| 56 | + |
| 57 | +```sql |
| 58 | +SELECT gender, RANK() |
| 59 | +OVER ( |
| 60 | + ORDER BY gender DESC |
| 61 | + ) |
| 62 | +AS rnk FROM accounts; |
| 63 | +``` |
| 64 | + |
| 65 | +| gender | rank |
| 66 | +:--- | :--- |
| 67 | +| M | 1 |
| 68 | +| M | 1 |
| 69 | +| M | 1 |
| 70 | +| F | 4 |
| 71 | + |
| 72 | +In this case, the next few ranks are skipped depending on the number of ties that occur. |
| 73 | + |
| 74 | +### ROW_NUMBER |
| 75 | + |
| 76 | +`ROW_NUMBER` assigns a number to each data row of the result set sequentially. The row number increases by 1 regardless of the fields specified in the `ORDER BY` list. |
| 77 | + |
| 78 | +```sql |
| 79 | +SELECT gender, balance, ROW_NUMBER() |
| 80 | +OVER ( |
| 81 | + PARTITION BY gender ORDER BY balance |
| 82 | + ) |
| 83 | +AS num FROM accounts; |
| 84 | +``` |
| 85 | + |
| 86 | +| gender | balance | num |
| 87 | +:--- | :--- |
| 88 | +| F | 32838 | 1 |
| 89 | +| M | 4180 | 1 |
| 90 | +| M | 5686 | 2 |
| 91 | +| M | 39225 | 3 |
| 92 | + |
| 93 | + |
| 94 | +### DENSE_RANK |
| 95 | + |
| 96 | +Similar to the `RANK` function, `DENSE_RANK` also assigns a ranking value to each row but without any gaps between the ranking values. |
| 97 | + |
| 98 | +```sql |
| 99 | +SELECT gender, DENSE_RANK() |
| 100 | +OVER ( |
| 101 | + ORDER BY gender DESC |
| 102 | + ) |
| 103 | +AS rnk FROM accounts; |
| 104 | +``` |
| 105 | + |
| 106 | +| gender | rank |
| 107 | +:--- | :--- |
| 108 | +| M | 1 |
| 109 | +| M | 1 |
| 110 | +| M | 1 |
| 111 | +| F | 2 |
0 commit comments