RANK_UNIQUE (window)

Syntax

RANK_UNIQUE( value [ , direction ] )
RANK_UNIQUE( 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 have different rank values. This means that rank values are sequential and different for all rows, always increasing by 1 for the next row.

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_DENSE, 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_UNIQUE(SUM([Orders]), "desc")RANK_UNIQUE(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_UNIQUE(SUM([Orders]) TOTAL)RANK_UNIQUE(SUM([Orders]) WITHIN [City])RANK_UNIQUE(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.