You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 16, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: docs/sql/window.md
+28-24Lines changed: 28 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ nav_order: 11
9
9
10
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
11
12
-
`PARTITION` and `ORDER` define the frame of data over which the calculations are made.
12
+
`PARTITION BY` and `ORDER BY` define the frame of data over which the calculation is made.
13
13
14
14
You can use window functions in the following three categories:
15
15
@@ -43,13 +43,35 @@ PUT _cluster/settings
43
43
44
44
## Ranking functions
45
45
46
-
Ranking functions assign an incremental rank to each row in the frame.
46
+
Ranking functions assign an incremental ranking value to each row in the frame.
47
47
48
-
If the ranking function is not used with the order of the data rows, the result is undetermined.
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. In this case, the next few ranks are skipped depending on the number of ties that occur.
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
49
71
50
72
### ROW_NUMBER
51
73
52
-
`ROW_NUMBER` assigns a number to each data row in a random order. As a special case, the row number is always increased by 1 regardless of the fields specified in the `ORDER BY` list.
74
+
`ROW_NUMBER` assigns a number to each data row of the result set sequentially. The row number is increases by 1 regardless of the fields specified in the `ORDER BY` list.
53
75
54
76
```sql
55
77
SELECT gender, balance, ROW_NUMBER()
@@ -66,35 +88,17 @@ AS num FROM accounts;
66
88
| M | 5686 | 2
67
89
| M | 39225 | 3
68
90
69
-
### RANK
70
-
71
-
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 are skipped depending on the number of ties.
72
-
73
-
```sql
74
-
SELECT gender, RANK()
75
-
OVER (
76
-
ORDER BY gender DESC
77
-
)
78
-
AS rank FROM accounts;
79
-
```
80
-
81
-
| gender | rank
82
-
:--- | :---
83
-
| M | 1
84
-
| M | 1
85
-
| M | 1
86
-
| F | 4
87
91
88
92
### DENSE_RANK
89
93
90
-
Similar to the `RANK` function, `DENSE_RANK` also assigns a rank to each row. The difference is there is no gap between the ranks.
94
+
Similar to the `RANK` function, `DENSE_RANK` also assigns a ranking value to each row but without any gaps between the ranking values.
0 commit comments