Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.

Commit 5cc92f6

Browse files
author
ashwinkumar12345
committed
added window functions
1 parent 611baa1 commit 5cc92f6

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

docs/sql/window.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ nav_order: 11
99

1010
A window function performs a calculation across a frame of data rows around the current row and finds a result for each row.
1111

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.
1313

1414
You can use window functions in the following three categories:
1515

@@ -43,13 +43,35 @@ PUT _cluster/settings
4343

4444
## Ranking functions
4545

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.
4747

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
4971

5072
### ROW_NUMBER
5173

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.
5375

5476
```sql
5577
SELECT gender, balance, ROW_NUMBER()
@@ -66,35 +88,17 @@ AS num FROM accounts;
6688
| M | 5686 | 2
6789
| M | 39225 | 3
6890

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
8791

8892
### DENSE_RANK
8993

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.
9195

9296
```sql
9397
SELECT gender, DENSE_RANK()
9498
OVER (
9599
ORDER BY gender DESC
96100
)
97-
AS rank FROM accounts;
101+
AS rnk FROM accounts;
98102
```
99103

100104
| gender | rank

0 commit comments

Comments
 (0)