Skip to main content

Analytic Functions

Overview

Analytic functions calculate an aggregate value based on a group of rows. Unlike aggregate functions, however, analytic functions can return multiple rows for each group. The functions covered in this section are:

CUME_DIST

CUME_DIST calculates the relative position of a specified value in a group of values.

Return Type

FLOAT(53)

Syntax

CUME_DIST( )  
OVER ( [ partition_by_clause ] order_by_clause )

Arguments

ArgumentDescriptionExample
OVERDetermines the partitioning and ordering of a rowset before the associated window function is applied.
PARTITION BYThis optional clause divides the result set into partitions to which the function is applied.
ORDER BYDetermines the logical order in which the operation is performed.Salary

Example

This example returns a randomly generated, 60-byte number.

SELECT 
Name,
Department,
CUME_DIST() OVER(ORDER BY Salary) AS percent_rank
FROM [Documentation].[Customer]

cume_dist

FIRST_VALUE

FIRST_VALUE returns the first value in an ordered set of values.

Return Type

Returns the same type as the expression.

Syntax

FIRST_VALUE ( [expression] ) [ IGNORE NULLS | RESPECT NULLS ]
OVER ( [partition_by] order_by [rows_range] )

Arguments

ArgumentDescriptionExample
expressionA column, subquery, or expression to return the first value of.Age
IGNORE/RESPECT NULLSAn optional argument that can be used to specify whether nulls will be included when calculating the first value. If no argument is included, RESPECT NULLS will be used by default.
PARTITION BYOptional. Divides the result set produced by the FROM clause into partitions to which the function is applied. If not specified, the function treats all rows of the query result set as a single group.
ORDER BYRequired. Determines the logical order in which the operation is performed.Salary
ROWS_RANGEOptional. Limits the rows within the partition by specifying start and end points.

Example

This example the first (lowest) value from the Salary column of the [HR].[Employees] table.

SELECT
FIRST_VALUE(Salary)
OVER(ORDER BY Salary)
AS BottomRange
FROM [HR].[Employee]

LAST_VALUE

LAST_VALUE returns the last value in an ordered set of values.

Return Type

Returns the same type as the expression.

Syntax

LAST_VALUE ( [expression] ) [ IGNORE NULLS | RESPECT NULLS ]
OVER ( [partition_by] order_by [rows_range] )

Arguments

ArgumentDescriptionExample
expressionA column, subquery, or expression to return the last value of.Age
IGNORE/RESPECT NULLSAn optional argument that can be used to specify whether nulls will be included when calculating the last value. If no argument is included, RESPECT NULLS will be used by default.
PARTITION BYOptional. Divides the result set produced by the FROM clause into partitions to which the function is applied. If not specified, the function treats all rows of the query result set as a single group.
ORDER BYRequired. Determines the logical order in which the operation is performed.Salary
ROWS_RANGEOptional. Limits the rows within the partition by specifying start and end points.

Example

This example the last (highest) value from the Salary column of the [HR].[Employees] table.

SELECT
LAST_VALUE(Salary)
OVER(ORDER BY Salary)
AS TopRange
FROM [HR].[Employee]