SQL SERVER – 2012 – Summary of All the Analytic Functions – MSDN and SQLAuthority

SQL Server 2012 introduced Analytic Functions for common window calculations. I collected this series to make them easier to learn.

A stepped ceramic display has a removable frame selecting part of one vessel group.

Before these functions, I often needed longer expressions for the same calculations. Native expressions made those common calculations easier to recognize. Native syntax improves clarity, while any performance advantage still needs measurement.

The eight functions in the original series

FunctionSQLAuthority articleMicrosoft reference (2012)
CUME_DISTCUME_DISTCUME_DIST
FIRST_VALUEFIRST_VALUEFIRST_VALUE
LAST_VALUELAST_VALUELAST_VALUE
LEADLEADLEAD
LAGLAGLAG
PERCENTILE_CONTPERCENTILE_CONTPERCENTILE_CONT
PERCENTILE_DISCPERCENTILE_DISCPERCENTILE_DISC
PERCENT_RANKPERCENT_RANKPERCENT_RANK
  • CUME_DIST and PERCENT_RANK describe relative position within an ordered partition.
  • FIRST_VALUE and LAST_VALUE return values from the applicable window frame.
  • LEAD and LAG access another ordered row without a self join for that expression.
  • PERCENTILE_CONT interpolates a percentile; PERCENTILE_DISC selects a discrete value.

I review partitioning, ordering, ties, NULL behavior and the frame for each function. LAST_VALUE deserves particular attention. With the default frame, it doesn’t automatically select the final row of the entire partition.

Keep the series history

The original appeared during the 2012 release-candidate period. Its RC0 download reference is historical, not a current installation recommendation. The FIRST_VALUE/LAST_VALUE, LEAD/LAG, and percentile puzzles remain linked below without new spoilers.

This series also brought a memorable surprise: my book sold out and became available again 48 hours later. That happened during the original publication period. I still remember the excitement around those new SQL Server features.

Reference: Analytic function reference.

Related reading

Window syntax is not the whole calculation, it is an expression whose ordering and frame still matter.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Function, SQL Server
Previous Post
SQL SERVER – Introduction to PERCENTILE_DISC() – Analytic Functions Introduced in SQL Server 2012
Next Post
SQL SERVER – DVM sys.dm_os_sys_info Column Name Changed in SQL Server 2012

Related Posts

7 Comments. Leave new

  • This page is in my bookmarks now …:)… Great series of SQL Server 2012 analytic functions.

    Regards,
    P.Anish Shenoy.

    Reply
  • Chirag Satasiya
    November 23, 2011 9:21 pm

    Hi pinal sir,
    I am expecting this post and here it is.

    Thank you for sharing.

    Regard$
    Chirag Satasiya

    Reply
  • Thanks Pinal, I am looking for the exact post…

    Appreciate your efforts.

    Regards,
    Suman Balguri

    Reply
  • hi sir i want to know how to count coloumns reapting the same values in table please help me out its attendence table and i want to count number of leave,prestent ,casual leave and all

    Reply
  • Hello Pinal,

    There is a commercial product which provides hundreds of analytic functions for SQL Server 2005, 2008 and 2012 – financial, statistical, mathematical etc. They are implemented as .NET SQL-CLR functions (permission level:safe), modeled mainly after the analytic functions found in Excel, but also from those found in R, Matlab, and Bloomberg.

    It is called XLeratorDB and can be found at westclintech.com

    Reply
  • thanks sweete

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.