windowing functions in sql server

14
Windowing Functions Dinesh Karunarathna B.Sc. (Eng.),MCTS – SQL Server Senior DBA – Pearson Lanka [email protected] http://www.dbbitsandpieces.blogspot.com/

Upload: sql-server-sri-lanka-user-group

Post on 18-Dec-2014

43 views

Category:

Technology


4 download

DESCRIPTION

201205 - Session 1 TITLE: Windowing Functions in SQL Server SPEAKER: Dinesh Karunarathna

TRANSCRIPT

Page 1: Windowing Functions in SQL Server

Windowing Functions

Dinesh KarunarathnaB.Sc. (Eng.),MCTS – SQL Server

Senior DBA – Pearson [email protected]://www.dbbitsandpieces.blogspot.com/

Page 2: Windowing Functions in SQL Server

IntroductionWhat are the windowing functions

Functions that operates/ manipulates on set of rows

Types of windowing functions Ranking functions - Returns a ranking value for

each row Aggregate functions - Performs calculations on set

of values Analytic functions - Compute moving averages,

running totals, percentages…

Improvements of windowing functions in SQL Server 2012

Page 3: Windowing Functions in SQL Server

New functionality in SQL Server 2005

OVER() Clause – Used to define a window or user specified set of rows within a query result set

PARTITION BY Clause - Divides the query result set into partitions

Ranking functions

Changes in aggregate functions

Page 4: Windowing Functions in SQL Server

Partitioning a data setPartitioned by LocationID

Partitioned by CustomerID

Page 5: Windowing Functions in SQL Server

Ranking functionsROW_NUMBER() -  Sequential number of a

row within a partition of a result setRANK() - Rank of each row within the partitionDENSE_RANK() - rank of rows within the

partition of a result set, without any gaps in the ranking

NTILE() - Distributes the rows in an ordered partition into a specified number of groups

Page 6: Windowing Functions in SQL Server

Demo…

Page 7: Windowing Functions in SQL Server

Aggregate functions

AVG() SUM() MIN() MAX() COUNT()….

Page 8: Windowing Functions in SQL Server

Aggregate functions cont…Enhancements in SQL Serve 2005

SUM(Column1) OVER (PARTITION BY Column2)

Enhancements in SQL Server 2012SUM(Column1) OVER (PARTITION BY

Column2 ORDER BY Column3)

Page 9: Windowing Functions in SQL Server

Demo…

Page 10: Windowing Functions in SQL Server

New features with SQL Server 2012Analytic functions

FIRST_VALUE()LAST_VALUE()LEAD()LAG()PERCENT_RANK()PERCENTILE_COUNT()PERCENTILE_DISC()CUME_DIST()

Page 11: Windowing Functions in SQL Server

New features with SQL Server 2012 cont …

Enhancements of Aggregate functions ODER BY with OVER() clause

Enhancements of defining the partition (window) ROWS/RANGE CURRENT ROW PRECEDING FOLLOWING UNBOUNDED

Page 12: Windowing Functions in SQL Server

Demo …

Page 13: Windowing Functions in SQL Server

Q & AWhich function can be best used to retrieve

daily increment?

List five new key words used to define a window of a dataset

Page 14: Windowing Functions in SQL Server

Thank you.