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

Commit f2a9b2b

Browse files
author
ashwinkumar12345
committed
added window functions
1 parent fab160e commit f2a9b2b

File tree

9 files changed

+144
-7
lines changed

9 files changed

+144
-7
lines changed

docs/sql/delete.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: default
33
title: Delete
44
parent: SQL
5-
nav_order: 11
5+
nav_order: 12
66
---
77

88

docs/sql/endpoints.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: default
33
title: Endpoint
44
parent: SQL
5-
nav_order: 12
5+
nav_order: 13
66
---
77

88

docs/sql/functions.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,54 @@ Function | Specification | Example
131131
if | `if(boolean, es_type, es_type) -> es_type` | `SELECT if(false, 0, 1) FROM my-index LIMIT 1`, `SELECT if(true, 0, 1) FROM my-index LIMIT 1`
132132
ifnull | `ifnull(es_type, es_type) -> es_type` | `SELECT ifnull('hello', 1) FROM my-index LIMIT 1`, `SELECT ifnull(null, 1) FROM my-index LIMIT 1`
133133
isnull | `isnull(es_type) -> integer` | `SELECT isnull(null) FROM my-index LIMIT 1`, `SELECT isnull(1) FROM my-index LIMIT 1`
134+
135+
## Ranking
136+
137+
Ranking functions assign an incremental rank to each row in the frame.
138+
139+
If the ranking function is not used with the window definition that defines the order of data rows, the result is undetermined.
140+
141+
### ROW_NUMBER
142+
143+
`ROW_NUMBER` assigns a row number to data rows 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.
144+
145+
```sql
146+
od> SELECT gender, balance, ROW_NUMBER() OVER(PARTITION BY gender ORDER BY balance) AS num FROM accounts;
147+
```
148+
149+
| gender | balance | num
150+
:--- | :---
151+
| F | 32838 | 1
152+
| M | 4180 | 1
153+
| M | 5686 | 2
154+
| M | 39225 | 3
155+
156+
### RANK
157+
158+
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.
159+
160+
```sql
161+
od> SELECT gender, RANK() OVER(ORDER BY gender DESC) AS rnk FROM accounts;
162+
```
163+
164+
| gender | rank
165+
:--- | :---
166+
| M | 1
167+
| M | 1
168+
| M | 1
169+
| F | 4
170+
171+
### DENSE_RANK
172+
173+
Similar to the `RANK` function, `DENSE_RANK` also assigns a rank to each row. The difference is there is no gap between the ranks.
174+
175+
```sql
176+
SELECT gender, DENSE_RANK() OVER(ORDER BY gender DESC) AS rnk FROM accounts;
177+
```
178+
179+
| gender | rank
180+
:--- | :---
181+
| M | 1
182+
| M | 1
183+
| M | 1
184+
| F | 2

docs/sql/limitation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: default
33
title: Limitations
44
parent: SQL
5-
nav_order: 17
5+
nav_order: 18
66
---
77

88
# Limitations

docs/sql/monitoring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: default
33
title: Monitoring
44
parent: SQL
5-
nav_order: 14
5+
nav_order: 15
66
---
77

88
# Monitoring

docs/sql/protocol.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: default
33
title: Protocol
44
parent: SQL
5-
nav_order: 13
5+
nav_order: 14
66
---
77

88
# Protocol

docs/sql/settings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: default
33
title: Settings
44
parent: SQL
5-
nav_order: 15
5+
nav_order: 16
66
---
77

88
# Settings

docs/sql/troubleshoot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: default
33
title: Troubleshooting
44
parent: SQL
5-
nav_order: 16
5+
nav_order: 17
66
---
77

88
# Troubleshooting

docs/sql/window.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)