@@ -476,9 +476,10 @@ WHERE
476
476
477
477
### dense_rank
478
478
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
+
482
483
The [ ` ORDER BY ` clause] ( #order-by-clause ) in the ` OVER ` clause determines
483
484
ranking order.
484
485
@@ -521,6 +522,33 @@ WHERE
521
522
522
523
{{% /influxdb/custom-timestamps %}}
523
524
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)
524
552
{{% /expand %}}
525
553
{{< /expand-wrapper >}}
526
554
@@ -632,9 +660,10 @@ WHERE
632
660
633
661
### rank
634
662
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
+
638
667
The [ ` ORDER BY ` clause] ( #order-by-clause ) in the ` OVER ` clause determines
639
668
ranking order.
640
669
@@ -675,6 +704,33 @@ WHERE
675
704
676
705
{{% /influxdb/custom-timestamps %}}
677
706
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)
678
734
{{% /expand %}}
679
735
{{< /expand-wrapper >}}
680
736
@@ -721,6 +777,33 @@ WHERE
721
777
722
778
{{% /influxdb/custom-timestamps %}}
723
779
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)
724
807
{{% /expand %}}
725
808
{{< /expand-wrapper >}}
726
809
0 commit comments