Skip to main content

Categories: Window

CUME_DIST

Returns the cumulative distribution of the current row within its partition as a value between 0.0 and 1.0. The distribution is the number of rows with a value less than or equal to the current row divided by the total number of rows in the partition. Requires an ORDER BY clause in the OVER window specification.

Syntax​

CUME_DIST() OVER ( [PARTITION BY partition_expression] ORDER BY order_expression) → double​

  • 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

CUME_DIST example
SELECT "Category", 
"Descript",
"DayOfWeek",
CUME_DIST()
OVER (
PARTITION BY "Category"
ORDER BY "DayOfWeek")
FROM Samples."samples.dremio.com"."SF_incidents2016.json"
-- Category, Descript, DayOfWeek, EXPR$3
-- ARSON, ARSON, Friday, 0.13636363636363635
-- EMBEZZLEMENT, EMBEZZLED VEHICLE, Friday, 0.18452380952380953
Calculate the cumulative distribution with tied values
SELECT val,
CUME_DIST()
OVER (
ORDER BY val)
FROM (VALUES (10), (20), (20), (30)) AS t(val)
-- val, EXPR$1
-- 10, 0.25
-- 20, 0.75
-- 20, 0.75
-- 30, 1.0
Calculate the cumulative distribution with a NULL value
SELECT name,
CUME_DIST()
OVER (
ORDER BY name)
FROM (VALUES ('Dremio'), (NULL), ('Cloud')) AS t(name)
-- name, EXPR$1
-- Cloud, 0.3333333333333333
-- Dremio, 0.6666666666666666
-- null, 1.0
Compare CUME_DIST and PERCENT_RANK
SELECT val,
CUME_DIST()
OVER (
ORDER BY val),
PERCENT_RANK()
OVER (
ORDER BY val)
FROM (VALUES (10), (20), (20), (30)) AS t(val)
-- val, EXPR$1, EXPR$2
-- 10, 0.25, 0.0
-- 20, 0.75, 0.3333333333333333
-- 20, 0.75, 0.3333333333333333
-- 30, 1.0, 1.0

Usage Notes​

This function does not support cumulative frame windows or sliding frame windows.