Skip to content

Commit 3c75645

Browse files
authored
Merge pull request #5860 from influxdata/5859-ranking-functions
fix Get started with monolith: query and processing engine
2 parents ea856bb + d7200f1 commit 3c75645

File tree

1 file changed

+89
-6
lines changed
  • content/shared/sql-reference/functions

1 file changed

+89
-6
lines changed

content/shared/sql-reference/functions/window.md

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,10 @@ WHERE
476476

477477
### dense_rank
478478

479-
Returns a rank for each row without gaps in the numbering.
480-
Unlike [rank()](#rank), this function assigns consecutive ranks even when values
481-
are identical.
479+
Returns the rank of the current row in its partition.
480+
Ranking is consecutive; assigns duplicate values the same rank number and the rank sequence continues
481+
with the next distinct value (unlike [`rank()`](#rank)).
482+
482483
The [`ORDER BY` clause](#order-by-clause) in the `OVER` clause determines
483484
ranking order.
484485

@@ -521,6 +522,33 @@ WHERE
521522

522523
{{% /influxdb/custom-timestamps %}}
523524

525+
{{% /expand %}}
526+
{{% expand "Compare `dense_rank`, `rank`, and `row_number` functions"%}}
527+
528+
Consider a table with duplicate ID values.
529+
The following query shows how each ranking function handles duplicate values:
530+
531+
```sql
532+
SELECT
533+
id,
534+
rank() OVER(ORDER BY id),
535+
dense_rank() OVER(ORDER BY id),
536+
row_number() OVER(ORDER BY id)
537+
FROM my_table;
538+
```
539+
540+
| ID | rank | dense_rank | row_number |
541+
|:----|-----:|-----------:|-----------:|
542+
| 1 | 1 | 1 | 1 |
543+
| 1 | 1 | 1 | 2 |
544+
| 1 | 1 | 1 | 3 |
545+
| 2 | 4 | 2 | 4 |
546+
547+
Key differences:
548+
549+
- [`rank()`](#rank) assigns the same rank to equal values but skips ranks for subsequent values
550+
- [`dense_rank()`](#dense_rank) assigns the same rank to equal values and uses consecutive ranks
551+
- [`row_number()`](#row_number) assigns unique sequential numbers regardless of value (non-deterministic)
524552
{{% /expand %}}
525553
{{< /expand-wrapper >}}
526554

@@ -632,9 +660,10 @@ WHERE
632660

633661
### rank
634662

635-
Returns the rank of the current row in its partition, allowing gaps between
636-
ranks. This function provides a ranking similar to [`row_number`](#row_number),
637-
but skips ranks for identical values.
663+
Returns the rank of the current row in its partition.
664+
For duplicate values, `rank` assigns them the same rank number, skips subsequent ranks (unlike [`dense_rank()`](#dense_rank)),
665+
and then continues ranking with the next distinct value.
666+
638667
The [`ORDER BY` clause](#order-by-clause) in the `OVER` clause determines
639668
ranking order.
640669

@@ -675,6 +704,33 @@ WHERE
675704

676705
{{% /influxdb/custom-timestamps %}}
677706

707+
{{% /expand %}}
708+
{{% expand "Compare `dense_rank`, `rank`, and `row_number` functions"%}}
709+
710+
Consider a table with duplicate ID values.
711+
The following query shows how each ranking function handles duplicate values:
712+
713+
```sql
714+
SELECT
715+
id,
716+
rank() OVER(ORDER BY id),
717+
dense_rank() OVER(ORDER BY id),
718+
row_number() OVER(ORDER BY id)
719+
FROM my_table;
720+
```
721+
722+
| ID | rank | dense_rank | row_number |
723+
|:----|-----:|-----------:|-----------:|
724+
| 1 | 1 | 1 | 1 |
725+
| 1 | 1 | 1 | 2 |
726+
| 1 | 1 | 1 | 3 |
727+
| 2 | 4 | 2 | 4 |
728+
729+
Key differences:
730+
731+
- [`rank()`](#rank) assigns the same rank to equal values but skips ranks for subsequent values
732+
- [`dense_rank()`](#dense_rank) assigns the same rank to equal values and uses consecutive ranks
733+
- [`row_number()`](#row_number) assigns unique sequential numbers regardless of value (non-deterministic)
678734
{{% /expand %}}
679735
{{< /expand-wrapper >}}
680736

@@ -721,6 +777,33 @@ WHERE
721777

722778
{{% /influxdb/custom-timestamps %}}
723779

780+
{{% /expand %}}
781+
{{% expand "Compare `dense_rank`, `rank`, and `row_number` functions"%}}
782+
783+
Consider a table with duplicate ID values.
784+
The following query shows how each ranking function handles duplicate values:
785+
786+
```sql
787+
SELECT
788+
id,
789+
rank() OVER(ORDER BY id),
790+
dense_rank() OVER(ORDER BY id),
791+
row_number() OVER(ORDER BY id)
792+
FROM my_table;
793+
```
794+
795+
| ID | rank | dense_rank | row_number |
796+
|:----|-----:|-----------:|-----------:|
797+
| 1 | 1 | 1 | 1 |
798+
| 1 | 1 | 1 | 2 |
799+
| 1 | 1 | 1 | 3 |
800+
| 2 | 4 | 2 | 4 |
801+
802+
Key differences:
803+
804+
- [`rank()`](#rank) assigns the same rank to equal values but skips ranks for subsequent values
805+
- [`dense_rank()`](#dense_rank) assigns the same rank to equal values and uses consecutive ranks
806+
- [`row_number()`](#row_number) assigns unique sequential numbers regardless of value (non-deterministic)
724807
{{% /expand %}}
725808
{{< /expand-wrapper >}}
726809

0 commit comments

Comments
 (0)