Skip to main content

Window Functions

Overview

Window functions perform calculations across a set of related rows while returning one result per input row. Unlike aggregate functions, window functions don't collapse rows into a single output.

Key characteristics:

  • Operate on a "window" of rows related to the current row
  • Return one value per input row (no grouping/collapsing)
  • Can access values from other rows in the window
  • Support partitioning and ordering for flexible calculations

Window Function Categories

Databend supports two main categories of window functions:

1. Dedicated Window Functions

These functions are specifically designed for window operations.

Ranking Functions:

FunctionDescriptionTies HandlingExample Output
ROW_NUMBERSequential numberingAlways unique1, 2, 3, 4, 5
RANKRanking with gapsSame rank, gaps after1, 2, 2, 4, 5
DENSE_RANKRanking without gapsSame rank, no gaps1, 2, 2, 3, 4

Distribution Functions:

FunctionDescriptionRangeExample Output
PERCENT_RANKRelative rank as percentage0.0 to 1.00.0, 0.25, 0.5, 0.75, 1.0
CUME_DISTCumulative distribution0.0 to 1.00.2, 0.4, 0.6, 0.8, 1.0
NTILEDivide into N buckets1 to N1, 1, 2, 2, 3, 3

Value Access Functions:

FunctionDescriptionUse Case
FIRST_VALUEFirst value in windowGet highest/earliest value
LAST_VALUELast value in windowGet lowest/latest value
NTH_VALUENth value in windowGet specific positioned value
LAGPrevious row valueCompare with previous
LEADNext row valueCompare with next

Aliases:

FunctionAlias For
FIRSTFIRST_VALUE
LASTLAST_VALUE

2. Aggregate Functions Used as Window Functions

These are standard aggregate functions that can be used with the OVER clause to perform window operations.

FunctionDescriptionWindow Frame SupportExample
SUMCalculates sum over windowSUM(sales) OVER (PARTITION BY region ORDER BY date)
AVGCalculates average over windowAVG(score) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
COUNTCounts rows over windowCOUNT(*) OVER (PARTITION BY department)
MINReturns minimum value in windowMIN(price) OVER (PARTITION BY category)
MAXReturns maximum value in windowMAX(price) OVER (PARTITION BY category)
ARRAY_AGGCollects values into arrayARRAY_AGG(product) OVER (PARTITION BY category)
STDDEV_POPPopulation standard deviationSTDDEV_POP(value) OVER (PARTITION BY group)
STDDEV_SAMPSample standard deviationSTDDEV_SAMP(value) OVER (PARTITION BY group)
MEDIANMedian valueMEDIAN(response_time) OVER (PARTITION BY server)

Conditional Variants

FunctionDescriptionWindow Frame SupportExample
COUNT_IFConditional countCOUNT_IF(status = 'complete') OVER (PARTITION BY dept)
SUM_IFConditional sumSUM_IF(amount, status = 'paid') OVER (PARTITION BY customer)
AVG_IFConditional averageAVG_IF(score, passed = true) OVER (PARTITION BY class)
MIN_IFConditional minimumMIN_IF(temp, location = 'outside') OVER (PARTITION BY day)
MAX_IFConditional maximumMAX_IF(speed, vehicle = 'car') OVER (PARTITION BY test)

Basic Syntax

All window functions follow this pattern:

FUNCTION() OVER (
[ PARTITION BY column ]
[ ORDER BY column ]
[ window_frame ]
)
  • PARTITION BY: Divides data into groups
  • ORDER BY: Sorts rows within each partition
  • window_frame: Defines which rows to include (optional)

Common Use Cases

  • Ranking: Create leaderboards and top-N lists
  • Analytics: Calculate running totals, moving averages, percentiles
  • Comparison: Compare current vs previous/next values
  • Grouping: Divide data into buckets without losing detail

For detailed syntax and examples, see individual function documentation above.