Categories: Window
DENSE_RANK
Returns the rank of the current row within its partition. Rows with equal values get the same rank, and the next distinct value gets the next consecutive rank, with no gaps. Requires an ORDER BY clause in the OVER window specification.
Syntax
DENSE_RANK() OVER ( [PARTITION BY partition_expression] ORDER BY order_expression) → bigint
- partition_expression (optional): An expression that groups rows into partitions.
- order_expression: An expression that specifies the order of the rows within each partition.
Examples
DENSE_RANK exampleSELECT "Category",
"Descript",
"DayOfWeek",
DENSE_RANK()
OVER (
PARTITION BY "Category"
ORDER BY "DayOfWeek")
FROM Samples."samples.dremio.com"."SF_incidents2016.json"
-- Category, Descript, DayOfWeek, EXPR$3
-- ARSON, ARSON, Friday, 1
-- ARSON, ARSON, Monday, 2
SELECT val,
DENSE_RANK()
OVER (
ORDER BY val)
FROM (VALUES (10), (20), (20), (30)) AS t(val)
-- val, EXPR$1
-- 10, 1
-- 20, 2
-- 20, 2
-- 30, 3
SELECT name,
val,
DENSE_RANK()
OVER (
PARTITION BY name
ORDER BY val)
FROM (VALUES ('Dremio', 10), ('Dremio', 30), ('Cloud', 20)) AS t(name, val)
-- name, val, EXPR$2
-- Cloud, 20, 1
-- Dremio, 10, 1
-- Dremio, 30, 2
SELECT val,
DENSE_RANK()
OVER (
ORDER BY val)
FROM (VALUES (10), (NULL), (20)) AS t(val)
-- val, EXPR$1
-- 10, 1
-- 20, 2
-- null, 3
SELECT val,
RANK()
OVER (
ORDER BY val),
DENSE_RANK()
OVER (
ORDER BY val),
ROW_NUMBER()
OVER (
ORDER BY val)
FROM (VALUES (10), (20), (20), (30)) AS t(val)
-- val, EXPR$1, EXPR$2, EXPR$3
-- 10, 1, 1, 1
-- 20, 2, 2, 2
-- 20, 2, 2, 3
-- 30, 4, 3, 4