RANK_DENSE (window)

Syntax

RANK_DENSE( value [ , direction ] )
RANK_DENSE( value [ , direction ]            [ TOTAL | WITHIN ... | AMONG ... ]            [ BEFORE FILTER BY ... ]          )

More info:

Description

Returns the rank of the current row if ordered by the given argument. Rows corresponding to the same value used for sorting have the same rank. If the first two rows both have rank of 1, then the next row (if it features a different value) will have rank 2, (rank without gaps).

If direction is "desc" or omitted, then ranking is done from greatest to least, if "asc", then from least to greatest.

See also RANK, RANK_UNIQUE, RANK_PERCENTILE.

Argument types:

  • valueBoolean | Date | Datetime | Fractional number | Integer | String | UUID
  • directionString

Return type: Integer

Note

Only constant values are accepted for the arguments (direction).

Examples

Example with two arguments

Source data

DateCityCategoryOrdersProfit
'2019-03-01''London''Office Supplies'8120.80
'2019-03-04''London''Office Supplies'2100.00
'2019-03-05''London''Furniture'1750.00
'2019-03-02''Moscow''Furniture'21250.50
'2019-03-03''Moscow''Office Supplies'485.00
'2019-03-01''San Francisco''Office Supplies'23723.00
'2019-03-01''San Francisco''Furniture'11000.00
'2019-03-03''San Francisco''Furniture'44000.00
'2019-03-02''Detroit''Furniture'53700.00
'2019-03-04''Detroit''Office Supplies'251200.00
'2019-03-04''Detroit''Furniture'23500.00

Grouped by [City].

Sorted by [City].

Result

[City]SUM([Orders])RANK_DENSE(SUM([Orders]), "desc")RANK_DENSE(SUM([Orders]), "asc")
'Detroit'3214
'London'1132
'Moscow'641
'San Francisco'2823
Example with grouping

Source data

DateCityCategoryOrdersProfit
'2019-03-01''London''Office Supplies'8120.80
'2019-03-04''London''Office Supplies'2100.00
'2019-03-05''London''Furniture'1750.00
'2019-03-02''Moscow''Furniture'21250.50
'2019-03-03''Moscow''Office Supplies'485.00
'2019-03-01''San Francisco''Office Supplies'23723.00
'2019-03-01''San Francisco''Furniture'11000.00
'2019-03-03''San Francisco''Furniture'44000.00
'2019-03-02''Detroit''Furniture'53700.00
'2019-03-04''Detroit''Office Supplies'251200.00
'2019-03-04''Detroit''Furniture'23500.00

Grouped by [City], [Category].

Sorted by [City], [Category].

Result

[City][Category]SUM([Orders])RANK_DENSE(SUM([Orders]) TOTAL)RANK_DENSE(SUM([Orders]) WITHIN [City])RANK_DENSE(SUM([Orders]) AMONG [City])
'Detroit''Furniture'7421
'Detroit''Office Supplies'25111
'London''Furniture'1824
'London''Office Supplies'10313
'Moscow''Furniture'2723
'Moscow''Office Supplies'4614
'San Francisco''Furniture'5522
'San Francisco''Office Supplies'23212

Data source support

ClickHouse 21.8, Microsoft SQL Server 2017 (14.0), MySQL 5.7, Oracle Database 12c (12.1), PostgreSQL 9.3.