SQL Tips
Window Functions – The OVER() Clause
June 4, 2014
©2014 Insurance Auto Auctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions.
Do not copy, reproduce or distribute without the prior written consent of Insurance Auto Auctions.
What is a Window Function?
©2014 Insurance Auto Auctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions.
Do not copy, reproduce or distribute without the prior written consent of Insurance Auto Auctions.
“A window function is applied to a set of rows defined
by a window descriptor and returns a single value for
each row from the underlying query.” – I. Ben-Gan
• Window Ranking functions (2005+)
• Window Aggregate Functions (2005+)
• Window Offset Functions (2012+)
• Window Distribution Functions (2012+)
The OVER() Clause
©2014 Insurance Auto Auctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions.
Do not copy, reproduce or distribute without the prior written consent of Insurance Auto Auctions.
• The OVER() clause is what defines the window
• Defines the set of rows that the function will
apply to
• PARTITION BY sets the boundaries for each set
of rows
• ORDER BY sets the order of the order for
ranking or offset functions within a given
partition
DEMOs
©2014 Insurance Auto Auctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions.
Do not copy, reproduce or distribute without the prior written consent of Insurance Auto Auctions.
• Ex 1 – 2005+ window aggregate using SUM()
and AVG()
• Ex 2 – 2005+ window ranking functions
– ROW_NUMBER()
– RANK()
– DENSE_RANK()
– NTILE(N)
DEMOs
©2014 Insurance Auto Auctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions.
Do not copy, reproduce or distribute without the prior written consent of Insurance Auto Auctions.
• Ex 3 – 2012+ window aggregate using ordering
and framing
– ROWS BETWEEN UNBOUNDED AND
{CURRENT|PRECEEDING |FOLLOWING} ROW
– ROWS BETWEEN N
{CURRENT|PRECEEDING|FOLLOWING} AND N
{CURRENT|PRECEEDING|FOLLOWING}
DEMOs
©2014 Insurance Auto Auctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions.
Do not copy, reproduce or distribute without the prior written consent of Insurance Auto Auctions.
• Ex 4 – 2012+ window offset functions
– LAG and LEAD
– FIRST_VALUE and LAST_VALUE
• Ex 5 – 2012+ window distribution functions
(statistical computations)
– Rank distribution
• PERCENT_RANK
• CUME_DIST
– Inverse Distribution
• PERCENTILE_DISC
• PERCENTILE_CONT
Sources
©2014 Insurance Auto Auctions, Inc..
Confidential and Proprietary Information of
Insurance Auto Auctions.
• http://sqlmag.com/sql-server-2012/how-use-microsoft-sql-
server-2012s-window-functions-part-1
• http://sqlmag.com/sql-server-2012/microsoft-sql-server-
2012-how-write-t-sql-window-functions-part-2
• http://sqlknowitall.com/?p=89

Window Functions

  • 1.
    SQL Tips Window Functions– The OVER() Clause June 4, 2014 ©2014 Insurance Auto Auctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions. Do not copy, reproduce or distribute without the prior written consent of Insurance Auto Auctions.
  • 2.
    What is aWindow Function? ©2014 Insurance Auto Auctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions. Do not copy, reproduce or distribute without the prior written consent of Insurance Auto Auctions. “A window function is applied to a set of rows defined by a window descriptor and returns a single value for each row from the underlying query.” – I. Ben-Gan • Window Ranking functions (2005+) • Window Aggregate Functions (2005+) • Window Offset Functions (2012+) • Window Distribution Functions (2012+)
  • 3.
    The OVER() Clause ©2014Insurance Auto Auctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions. Do not copy, reproduce or distribute without the prior written consent of Insurance Auto Auctions. • The OVER() clause is what defines the window • Defines the set of rows that the function will apply to • PARTITION BY sets the boundaries for each set of rows • ORDER BY sets the order of the order for ranking or offset functions within a given partition
  • 4.
    DEMOs ©2014 Insurance AutoAuctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions. Do not copy, reproduce or distribute without the prior written consent of Insurance Auto Auctions. • Ex 1 – 2005+ window aggregate using SUM() and AVG() • Ex 2 – 2005+ window ranking functions – ROW_NUMBER() – RANK() – DENSE_RANK() – NTILE(N)
  • 5.
    DEMOs ©2014 Insurance AutoAuctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions. Do not copy, reproduce or distribute without the prior written consent of Insurance Auto Auctions. • Ex 3 – 2012+ window aggregate using ordering and framing – ROWS BETWEEN UNBOUNDED AND {CURRENT|PRECEEDING |FOLLOWING} ROW – ROWS BETWEEN N {CURRENT|PRECEEDING|FOLLOWING} AND N {CURRENT|PRECEEDING|FOLLOWING}
  • 6.
    DEMOs ©2014 Insurance AutoAuctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions. Do not copy, reproduce or distribute without the prior written consent of Insurance Auto Auctions. • Ex 4 – 2012+ window offset functions – LAG and LEAD – FIRST_VALUE and LAST_VALUE • Ex 5 – 2012+ window distribution functions (statistical computations) – Rank distribution • PERCENT_RANK • CUME_DIST – Inverse Distribution • PERCENTILE_DISC • PERCENTILE_CONT
  • 7.
    Sources ©2014 Insurance AutoAuctions, Inc.. Confidential and Proprietary Information of Insurance Auto Auctions. • http://sqlmag.com/sql-server-2012/how-use-microsoft-sql- server-2012s-window-functions-part-1 • http://sqlmag.com/sql-server-2012/microsoft-sql-server- 2012-how-write-t-sql-window-functions-part-2 • http://sqlknowitall.com/?p=89