SlideShare una empresa de Scribd logo
1 de 34
Descargar para leer sin conexión
SQL Server Windowing
Functions
Enrique Catalá Bañuls
Mentor, SolidQ
MAP 2012 – Microsoft Technical Ranger – Microsoft Certified Trainer
ecatala@solidq.com
Twitter: @enriquecatala
© 2012 SolidQ
Enrique Catalá Bañuls
•Computer engineer
•Mentor at SolidQ in the relational engine team
•Microsoft Technical Ranger
•Microsoft Active Professional
•Microsoft Certified Trainer
© 2012 SolidQ
ecatala@solidq.com
Twitter: @enriquecatala
3© 2012 SolidQ
About SolidQ…
• SolidQ sets the standard for information management services by providing the world's
most trusted and reliable business intelligence, data management, collaboration and custom
solutions for Microsoft's cloud and on-premise platforms. SolidQ is recognized as an expert
among its peers:
• 80 of the world’s top information experts in our BI and data management
team, including more than 30 SQL MVPs
• Authored over 30 industry leading books
• Frequent speakers at local and international SQL Server technical
conferences such as TechEd around the world, the BI Conference, SQL PASS
Summit, and SQL Saturdays
• Serving over 800 clients in 22 countries
• We are so confident in the capabilities of our team and the value we
provide, we unconditionally guarantee your satisfaction with our services.
• For more information visit www.solidq.com
SolidQTM Journal
• Free, monthly e-magazine providing answers you need to be successful with
your Microsoft SQL Server, Business Intelligence, and software development
solutions
• Featuring in-depth technical articles, real-life case studies, practical columns,
and seasoned perspectives from our lineup of influential global experts—
both inside SolidQ and around the community
• Helping busy IT pros make the best decisions about which technologies to
use when and where, how to get the most out of their systems, and how to
be more productive and effective in their organizations
• Read today at www.solidq.com/sqj
Real Practices for
the Real World
© 2012 SolidQ – Think fast, move fast 4
Objectives of this session
•Introduction
•The past, the present and the future
•Window Functions
• Why do we need window functions?
• Syntax
• Performance
• Why almost complete?
•Q&A
5 |© 2012 SolidQ
Window Functions
•Window function
• function applied to a set of rows defined by a window
descriptor and returns a single value for each row
from the underlying query
•Window descriptor
• Define which rows will be used with the function
© 2012 SolidQ
Objectives of this session
•Introduction
•The past, the present and the future
•Window Functions
• Why do we need window functions?
• Syntax
• Performance
• Why almost complete?
•Q&A
7 |© 2012 SolidQ
The past, the present and the future
•The SQL 2000 way
• SQL Server 2000 does not provide any syntax to
support windowing functions
•The SQL 2005 way
• SQL Server 2005 to 2008R2 introduced a partial
implementation of the windowing functions using the
OVER clause
•The SQL 2012 way
• SQL Server 2012 will have an almost complete
implementation of the windowing functions
© 2012 SolidQ
The SQL 2005-2008R2 way
•SQL Server 2005 introduces
• OVER clause (partially implemented)
• New window functions
o ROW_NUMBER()
o RANK()
o DENSE_RANK()
o NTILE()
•SQL Server 2008/2008R2 added no new
implementations in this particular area
© 2012 SolidQ
The SQL 2005-2008R2 way
•OVER clause
•Two different implementations depending on
type of windowing
• Aggregate window functions does not provide the
ORDER BY clause
© 2012 SolidQ
The SQL 2012 way
• SQL Server 2012 are getting close to the full
implementation of Windowing functions
• SQL Server 2012 introduces:
• OVER clause almost complete
o Order by
o Window Frame
• 8 new window functions
o LAG(), LEAD()
o FIRST_VALUE(), LAST_VALUE()
o CUME_DIST()
o PERCENT_RANK()
o PERCENTILE_DISC(), PERCENTILE_COUNT()
© 2012 SolidQ
OVER clause syntax
•SQL Server 2005/2008/R2
• SQL Server 2012
© 2012 SolidQ
Objectives of this session
•Introduction
•The past, the present and the future
•Window Functions
• Why do we need window functions?
• Syntax
• Performance
• Why almost complete?
•Q&A
13 |© 2012 SolidQ
Why do we need windows functions?
• Why do we need windows functions?
• What if we want to get the sum and the value
column? (group and detail)
• If we are in SQL 2005/2008/2008R2…am i correct with
this approach?
select id_table, value,
sum(value) as [sum(value)]
from table1 group by id_table
Msg 8120, Level 16, State 1, Line 1
Column 'table1.value' is invalid in the select list because
it is not contained in either an aggregate function or the
GROUP BY clause.
id_table value
1 1
2 1
2 2
3 1
3 2
3 3
SUM(value)
1
3
6
id_table value sum(value)
1 1 1
2 1 3
2 2 3
3 1 6
3 2 6
3 3 6
Select sum(value) as [sum(value)]
from table1 group by id_table
© 2012 SolidQ
Why do we need windows functions?
•Now we know that the OVER clause exists…
•This must be our solution
select id_table,
value,
sum(value) over(partition by id_table)
from table1
id_table value sum(value)
1 1 1
2 1 3
2 2 3
3 1 6
3 2 6
3 3 6
© 2012 SolidQ
New syntax to solve old
problems
• Partitioning
• Ordering
• Slicing/framing
© 2012 SolidQ
Key concepts: All together
Partition
UNBOUNDED
FOLLOWING
UNBOUNDED
PRECEDING
CURRENT
ROW
© 2012 SolidQ
Key concepts
© 2012 SolidQ
Key concepts: Partition
•Partition is like a group of
rows with similar
“characteristics” inside a set
© 2012 SolidQ
Key concepts: Partition
•Partition is like a group of
rows with similar
“characteristics” inside a set
© 2012 SolidQ
Key concepts: Slicing/Framing
• RANGE/ROWS
• ROWS | RANGE BETWEEN <B1> AND <B2>
• ROWS | RANGE <B1>
© 2012 SolidQ
Key concepts: Slicing/Framing
• B1 and B2 can be
• UNBOUNDED PRECEDING
• UNBOUNDED FOLLOWING
• CURRENT ROW
• For “ROWS” clause only
o <scalar expression> PRECEDING
o <sclara expression> FOLLOWING
• Note
• B1 <= B2 or NULL will be returned
o Except in COUNT() that 0 will be returned
© 2012 SolidQ
Key concepts: All together
Partition
UNBOUNDED
FOLLOWING
UNBOUNDED
PRECEDING
CURRENT
ROW
© 2012 SolidQ
New windowing functions
• New analytic functions in SQL 2012
• offset
• LAG()
• LEAD()
• FIRST_VALUE()
• LAST_VALUE()
• Distribution
• PERCENT_RANK()
• CUME_DIST()
• PERCENTILE_CONT()
• PERCENTILE_DIST()
© 2012 SolidQ
DEMO 1: NEW FUNCTIONS
AND SYNTAX
Basics on framing, RANGE vs ROWS, new window functions
© 2012 SolidQ
Objectives of this session
•Introduction
•The past, the present and the future
•Window Functions
• Why do we need window functions?
• Syntax
• Performance
• Why almost complete?
•Q&A
26 |© 2012 SolidQ
Objectives of this session
•Introduction
•The past, the present and the future
•Window Functions
• Why do we need window functions?
• Syntax
• Performance
• Why almost complete?
•Q&A
27 |© 2012 SolidQ
Performance: IN-MEMORY vs ON
DISK
• Directly affected by Window Spool Operator
• Used to store the framing data
• In-memory vs disk-based worktable
• In-memory worktable
• Fastest
• Prerequisites: Framing defined using ROWS and working
with <10k rows
• Disk-based worktable
• Default
• Used when the window was defined using RANGE
• Used when the windows was defined using ROWS and the
frame has more than 10k rows
© 2012 SolidQ
Performance: Indexing
•Index order by the window partitioning
elements and then by the order elements
•Include the rest of the columns
•Optimized execution operators:
1. Scan
2. Segment
3. Window spool
4. Aggregate
© 2012 SolidQ
© 2012 SolidQ
DEMO 2: PERFORMANCE
Running Totals, in memory vs on disk, …
© 2012 SolidQ
Why almost complete?
• RANGE only supports
• UNBOUNDED PRECEDING
• UNBOUNDED FOLLOWING
• CURRENT ROW
• Ranking functions does not support framing:
• ROW_NUMBER, RANK, NTILE,DENSE_RANK,
PERCENTILE_DIST, PERCENTILE_CONT
• Not implemented
• WINDOW aliases
• Clauses: NULLs FIRST and NULLs LAST
© 2012 SolidQ
Objectives of this session
•Introduction
•The past, the present and the future
•Window Functions
• Why do we need window functions?
• Syntax
• Performance
• Why almost complete?
•Q&A
32 |
THANK YOU!
Enrique Catalá Bañuls
Mentor, SolidQ
MAP 2012 – Microsoft Technical Ranger – Microsoft Certified Trainer
ecatala@solidq.com
Twitter: @enriquecatala
Sql server windowing functions

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Database Triggers
Database TriggersDatabase Triggers
Database Triggers
 
Triggers
TriggersTriggers
Triggers
 
SQL commands
SQL commandsSQL commands
SQL commands
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql joins inner join self join outer joins
Sql joins inner join self join outer joinsSql joins inner join self join outer joins
Sql joins inner join self join outer joins
 
SQL for interview
SQL for interviewSQL for interview
SQL for interview
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
PL/SQL - CURSORS
PL/SQL - CURSORSPL/SQL - CURSORS
PL/SQL - CURSORS
 

Destacado

A Window into Your Data: T-SQL Window Functions
A Window into Your Data: T-SQL Window FunctionsA Window into Your Data: T-SQL Window Functions
A Window into Your Data: T-SQL Window FunctionsSteve Hughes
 
Mastering T-SQL Window Functions
Mastering T-SQL Window FunctionsMastering T-SQL Window Functions
Mastering T-SQL Window FunctionsJose Rivera Miranda
 
Fast transition to sql server 2012 from mssql 2005 2008 for developers - Dav...
Fast transition to sql server 2012 from mssql 2005 2008 for  developers - Dav...Fast transition to sql server 2012 from mssql 2005 2008 for  developers - Dav...
Fast transition to sql server 2012 from mssql 2005 2008 for developers - Dav...sqlserver.co.il
 
Window functions in MariaDB 10.2
Window functions in MariaDB 10.2Window functions in MariaDB 10.2
Window functions in MariaDB 10.2Sergey Petrunya
 
Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)
Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)
Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)Sergey Petrunya
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsZohar Elkayam
 
SQL Server - Constraints
SQL Server - ConstraintsSQL Server - Constraints
SQL Server - ConstraintsAaron Buma
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 Richie Rump
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsZohar Elkayam
 
10 SQL Tricks that You Didn't Think Were Possible
10 SQL Tricks that You Didn't Think Were Possible10 SQL Tricks that You Didn't Think Were Possible
10 SQL Tricks that You Didn't Think Were PossibleLukas Eder
 

Destacado (11)

SQL Windowing
SQL WindowingSQL Windowing
SQL Windowing
 
A Window into Your Data: T-SQL Window Functions
A Window into Your Data: T-SQL Window FunctionsA Window into Your Data: T-SQL Window Functions
A Window into Your Data: T-SQL Window Functions
 
Mastering T-SQL Window Functions
Mastering T-SQL Window FunctionsMastering T-SQL Window Functions
Mastering T-SQL Window Functions
 
Fast transition to sql server 2012 from mssql 2005 2008 for developers - Dav...
Fast transition to sql server 2012 from mssql 2005 2008 for  developers - Dav...Fast transition to sql server 2012 from mssql 2005 2008 for  developers - Dav...
Fast transition to sql server 2012 from mssql 2005 2008 for developers - Dav...
 
Window functions in MariaDB 10.2
Window functions in MariaDB 10.2Window functions in MariaDB 10.2
Window functions in MariaDB 10.2
 
Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)
Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)
Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
 
SQL Server - Constraints
SQL Server - ConstraintsSQL Server - Constraints
SQL Server - Constraints
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
 
10 SQL Tricks that You Didn't Think Were Possible
10 SQL Tricks that You Didn't Think Were Possible10 SQL Tricks that You Didn't Think Were Possible
10 SQL Tricks that You Didn't Think Were Possible
 

Similar a Sql server windowing functions

SQL Server Workshop for Developers - Visual Studio Live! NY 2012
SQL Server Workshop for Developers - Visual Studio Live! NY 2012SQL Server Workshop for Developers - Visual Studio Live! NY 2012
SQL Server Workshop for Developers - Visual Studio Live! NY 2012Andrew Brust
 
Directions NA Choosing the best possible Azure platform for NAV
Directions NA Choosing the best possible Azure platform for NAVDirections NA Choosing the best possible Azure platform for NAV
Directions NA Choosing the best possible Azure platform for NAVAleksandar Totovic
 
Sage Summit 2013: Sage 300 ERP Diagnostic Tools
Sage Summit 2013: Sage 300 ERP Diagnostic ToolsSage Summit 2013: Sage 300 ERP Diagnostic Tools
Sage Summit 2013: Sage 300 ERP Diagnostic ToolsSage 300 ERP CS
 
Automation & Cloud Evolution - Long View VMware Forum Calgary January 21 2014
Automation & Cloud Evolution - Long View VMware Forum Calgary January 21 2014Automation & Cloud Evolution - Long View VMware Forum Calgary January 21 2014
Automation & Cloud Evolution - Long View VMware Forum Calgary January 21 2014James Charter
 
JAVA_PLSQL_(9 years)_resume
JAVA_PLSQL_(9 years)_resumeJAVA_PLSQL_(9 years)_resume
JAVA_PLSQL_(9 years)_resumesumanta banerjee
 
SAP ABAP Online Training
SAP ABAP Online TrainingSAP ABAP Online Training
SAP ABAP Online TrainingNagendra Kumar
 
New in Visual Studio and TFS 2013
New in Visual Studio and TFS 2013New in Visual Studio and TFS 2013
New in Visual Studio and TFS 2013Tung Nguyen Thanh
 
Performance dreams of sql server 2014
Performance dreams of sql server 2014Performance dreams of sql server 2014
Performance dreams of sql server 2014Shehap Elnagar
 
Simplifying MySQL, Pre-FOSDEM MySQL Days, Brussels, January 30, 2020.
Simplifying MySQL, Pre-FOSDEM MySQL Days, Brussels, January 30, 2020.Simplifying MySQL, Pre-FOSDEM MySQL Days, Brussels, January 30, 2020.
Simplifying MySQL, Pre-FOSDEM MySQL Days, Brussels, January 30, 2020.Geir Høydalsvik
 
Optimizing Access with SQL Server
Optimizing Access with SQL ServerOptimizing Access with SQL Server
Optimizing Access with SQL ServerPRPASS Chapter
 
What's New in Oracle BI for Developers
What's New in Oracle BI for DevelopersWhat's New in Oracle BI for Developers
What's New in Oracle BI for DevelopersDatavail
 
Sql developer - Powerful Free tool for Developers and DBA's
Sql developer - Powerful Free tool for Developers and DBA'sSql developer - Powerful Free tool for Developers and DBA's
Sql developer - Powerful Free tool for Developers and DBA'sNavneet Upneja
 
OOW15 - Standards-Based Desktop Integration in Oracle E-Business Suite
OOW15 - Standards-Based Desktop Integration in Oracle E-Business SuiteOOW15 - Standards-Based Desktop Integration in Oracle E-Business Suite
OOW15 - Standards-Based Desktop Integration in Oracle E-Business Suitevasuballa
 

Similar a Sql server windowing functions (20)

Using T-SQL
Using T-SQL Using T-SQL
Using T-SQL
 
SQL Server Workshop for Developers - Visual Studio Live! NY 2012
SQL Server Workshop for Developers - Visual Studio Live! NY 2012SQL Server Workshop for Developers - Visual Studio Live! NY 2012
SQL Server Workshop for Developers - Visual Studio Live! NY 2012
 
Plantilla oracle
Plantilla oraclePlantilla oracle
Plantilla oracle
 
Directions NA Choosing the best possible Azure platform for NAV
Directions NA Choosing the best possible Azure platform for NAVDirections NA Choosing the best possible Azure platform for NAV
Directions NA Choosing the best possible Azure platform for NAV
 
ow.ppt
ow.pptow.ppt
ow.ppt
 
ow.ppt
ow.pptow.ppt
ow.ppt
 
Ow
OwOw
Ow
 
Sage Summit 2013: Sage 300 ERP Diagnostic Tools
Sage Summit 2013: Sage 300 ERP Diagnostic ToolsSage Summit 2013: Sage 300 ERP Diagnostic Tools
Sage Summit 2013: Sage 300 ERP Diagnostic Tools
 
Automation & Cloud Evolution - Long View VMware Forum Calgary January 21 2014
Automation & Cloud Evolution - Long View VMware Forum Calgary January 21 2014Automation & Cloud Evolution - Long View VMware Forum Calgary January 21 2014
Automation & Cloud Evolution - Long View VMware Forum Calgary January 21 2014
 
JAVA_PLSQL_(9 years)_resume
JAVA_PLSQL_(9 years)_resumeJAVA_PLSQL_(9 years)_resume
JAVA_PLSQL_(9 years)_resume
 
SAP ABAP Online Training
SAP ABAP Online TrainingSAP ABAP Online Training
SAP ABAP Online Training
 
Hello my name is DAX
Hello my name is DAXHello my name is DAX
Hello my name is DAX
 
New in Visual Studio and TFS 2013
New in Visual Studio and TFS 2013New in Visual Studio and TFS 2013
New in Visual Studio and TFS 2013
 
Performance dreams of sql server 2014
Performance dreams of sql server 2014Performance dreams of sql server 2014
Performance dreams of sql server 2014
 
SAP ABAP Online Training
SAP ABAP Online TrainingSAP ABAP Online Training
SAP ABAP Online Training
 
Simplifying MySQL, Pre-FOSDEM MySQL Days, Brussels, January 30, 2020.
Simplifying MySQL, Pre-FOSDEM MySQL Days, Brussels, January 30, 2020.Simplifying MySQL, Pre-FOSDEM MySQL Days, Brussels, January 30, 2020.
Simplifying MySQL, Pre-FOSDEM MySQL Days, Brussels, January 30, 2020.
 
Optimizing Access with SQL Server
Optimizing Access with SQL ServerOptimizing Access with SQL Server
Optimizing Access with SQL Server
 
What's New in Oracle BI for Developers
What's New in Oracle BI for DevelopersWhat's New in Oracle BI for Developers
What's New in Oracle BI for Developers
 
Sql developer - Powerful Free tool for Developers and DBA's
Sql developer - Powerful Free tool for Developers and DBA'sSql developer - Powerful Free tool for Developers and DBA's
Sql developer - Powerful Free tool for Developers and DBA's
 
OOW15 - Standards-Based Desktop Integration in Oracle E-Business Suite
OOW15 - Standards-Based Desktop Integration in Oracle E-Business SuiteOOW15 - Standards-Based Desktop Integration in Oracle E-Business Suite
OOW15 - Standards-Based Desktop Integration in Oracle E-Business Suite
 

Más de Enrique Catala Bañuls

Sql server ha muerto, larga vida a sql server
Sql server ha muerto, larga vida a sql serverSql server ha muerto, larga vida a sql server
Sql server ha muerto, larga vida a sql serverEnrique Catala Bañuls
 
Capas de acceso a datos .net escalables de verdad contra SQL Server
Capas de acceso a datos .net escalables de verdad contra SQL ServerCapas de acceso a datos .net escalables de verdad contra SQL Server
Capas de acceso a datos .net escalables de verdad contra SQL ServerEnrique Catala Bañuls
 
Aplicando R al análisis de rendimiento de un servidor
Aplicando R al análisis de rendimiento de un servidorAplicando R al análisis de rendimiento de un servidor
Aplicando R al análisis de rendimiento de un servidorEnrique Catala Bañuls
 
Técnicas avanzadas para resolver tus problemas de sql server
Técnicas avanzadas para resolver tus problemas de sql serverTécnicas avanzadas para resolver tus problemas de sql server
Técnicas avanzadas para resolver tus problemas de sql serverEnrique Catala Bañuls
 
Capas de acceso a datos .NET escalables de verdad: el batido perfecto para el...
Capas de acceso a datos .NET escalables de verdad: el batido perfecto para el...Capas de acceso a datos .NET escalables de verdad: el batido perfecto para el...
Capas de acceso a datos .NET escalables de verdad: el batido perfecto para el...Enrique Catala Bañuls
 
Planes de ejecución 3.0 sql 2016 y v next
Planes de ejecución 3.0 sql 2016 y v nextPlanes de ejecución 3.0 sql 2016 y v next
Planes de ejecución 3.0 sql 2016 y v nextEnrique Catala Bañuls
 
Aplicando R al análisis de rendimiento de un servidor
Aplicando R al análisis de rendimiento de un servidorAplicando R al análisis de rendimiento de un servidor
Aplicando R al análisis de rendimiento de un servidorEnrique Catala Bañuls
 
Sql server 2016 novedades para desarrolladores
Sql server 2016 novedades para desarrolladoresSql server 2016 novedades para desarrolladores
Sql server 2016 novedades para desarrolladoresEnrique Catala Bañuls
 
Dawarehouse como servicio en azure (sqldw)
Dawarehouse como servicio en azure (sqldw)Dawarehouse como servicio en azure (sqldw)
Dawarehouse como servicio en azure (sqldw)Enrique Catala Bañuls
 
Datawarehouse como servicio en azure (sqldw)
Datawarehouse como servicio en azure (sqldw)Datawarehouse como servicio en azure (sqldw)
Datawarehouse como servicio en azure (sqldw)Enrique Catala Bañuls
 
Como hacer tuning a capas de acceso a datos en .NET (dotNetConference2016)
Como hacer tuning a capas de acceso a datos en .NET (dotNetConference2016)Como hacer tuning a capas de acceso a datos en .NET (dotNetConference2016)
Como hacer tuning a capas de acceso a datos en .NET (dotNetConference2016)Enrique Catala Bañuls
 
Como leer planes de ejecución - edición 2015
Como leer planes de ejecución - edición 2015Como leer planes de ejecución - edición 2015
Como leer planes de ejecución - edición 2015Enrique Catala Bañuls
 

Más de Enrique Catala Bañuls (20)

Sql server ha muerto, larga vida a sql server
Sql server ha muerto, larga vida a sql serverSql server ha muerto, larga vida a sql server
Sql server ha muerto, larga vida a sql server
 
Capas de acceso a datos .net escalables de verdad contra SQL Server
Capas de acceso a datos .net escalables de verdad contra SQL ServerCapas de acceso a datos .net escalables de verdad contra SQL Server
Capas de acceso a datos .net escalables de verdad contra SQL Server
 
Paralelismo en SQL Server
Paralelismo en SQL ServerParalelismo en SQL Server
Paralelismo en SQL Server
 
Aplicando R al análisis de rendimiento de un servidor
Aplicando R al análisis de rendimiento de un servidorAplicando R al análisis de rendimiento de un servidor
Aplicando R al análisis de rendimiento de un servidor
 
Técnicas avanzadas para resolver tus problemas de sql server
Técnicas avanzadas para resolver tus problemas de sql serverTécnicas avanzadas para resolver tus problemas de sql server
Técnicas avanzadas para resolver tus problemas de sql server
 
Capas de acceso a datos .NET escalables de verdad: el batido perfecto para el...
Capas de acceso a datos .NET escalables de verdad: el batido perfecto para el...Capas de acceso a datos .NET escalables de verdad: el batido perfecto para el...
Capas de acceso a datos .NET escalables de verdad: el batido perfecto para el...
 
Planes de ejecución 3.0 sql 2016 y v next
Planes de ejecución 3.0 sql 2016 y v nextPlanes de ejecución 3.0 sql 2016 y v next
Planes de ejecución 3.0 sql 2016 y v next
 
Paralelismo en sql server
Paralelismo en sql serverParalelismo en sql server
Paralelismo en sql server
 
Aplicando R al análisis de rendimiento de un servidor
Aplicando R al análisis de rendimiento de un servidorAplicando R al análisis de rendimiento de un servidor
Aplicando R al análisis de rendimiento de un servidor
 
Query store
Query storeQuery store
Query store
 
Planes de ejecucion 2016
Planes de ejecucion 2016Planes de ejecucion 2016
Planes de ejecucion 2016
 
Sql server 2016 novedades para desarrolladores
Sql server 2016 novedades para desarrolladoresSql server 2016 novedades para desarrolladores
Sql server 2016 novedades para desarrolladores
 
Dawarehouse como servicio en azure (sqldw)
Dawarehouse como servicio en azure (sqldw)Dawarehouse como servicio en azure (sqldw)
Dawarehouse como servicio en azure (sqldw)
 
Query store
Query storeQuery store
Query store
 
Planes de ejecucion 2
Planes de ejecucion 2Planes de ejecucion 2
Planes de ejecucion 2
 
Planes de ejecucion 1
Planes de ejecucion 1Planes de ejecucion 1
Planes de ejecucion 1
 
Migración a sql server 2016
Migración a sql server 2016Migración a sql server 2016
Migración a sql server 2016
 
Datawarehouse como servicio en azure (sqldw)
Datawarehouse como servicio en azure (sqldw)Datawarehouse como servicio en azure (sqldw)
Datawarehouse como servicio en azure (sqldw)
 
Como hacer tuning a capas de acceso a datos en .NET (dotNetConference2016)
Como hacer tuning a capas de acceso a datos en .NET (dotNetConference2016)Como hacer tuning a capas de acceso a datos en .NET (dotNetConference2016)
Como hacer tuning a capas de acceso a datos en .NET (dotNetConference2016)
 
Como leer planes de ejecución - edición 2015
Como leer planes de ejecución - edición 2015Como leer planes de ejecución - edición 2015
Como leer planes de ejecución - edición 2015
 

Último

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Último (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Sql server windowing functions

  • 1. SQL Server Windowing Functions Enrique Catalá Bañuls Mentor, SolidQ MAP 2012 – Microsoft Technical Ranger – Microsoft Certified Trainer ecatala@solidq.com Twitter: @enriquecatala © 2012 SolidQ
  • 2. Enrique Catalá Bañuls •Computer engineer •Mentor at SolidQ in the relational engine team •Microsoft Technical Ranger •Microsoft Active Professional •Microsoft Certified Trainer © 2012 SolidQ ecatala@solidq.com Twitter: @enriquecatala
  • 3. 3© 2012 SolidQ About SolidQ… • SolidQ sets the standard for information management services by providing the world's most trusted and reliable business intelligence, data management, collaboration and custom solutions for Microsoft's cloud and on-premise platforms. SolidQ is recognized as an expert among its peers: • 80 of the world’s top information experts in our BI and data management team, including more than 30 SQL MVPs • Authored over 30 industry leading books • Frequent speakers at local and international SQL Server technical conferences such as TechEd around the world, the BI Conference, SQL PASS Summit, and SQL Saturdays • Serving over 800 clients in 22 countries • We are so confident in the capabilities of our team and the value we provide, we unconditionally guarantee your satisfaction with our services. • For more information visit www.solidq.com
  • 4. SolidQTM Journal • Free, monthly e-magazine providing answers you need to be successful with your Microsoft SQL Server, Business Intelligence, and software development solutions • Featuring in-depth technical articles, real-life case studies, practical columns, and seasoned perspectives from our lineup of influential global experts— both inside SolidQ and around the community • Helping busy IT pros make the best decisions about which technologies to use when and where, how to get the most out of their systems, and how to be more productive and effective in their organizations • Read today at www.solidq.com/sqj Real Practices for the Real World © 2012 SolidQ – Think fast, move fast 4
  • 5. Objectives of this session •Introduction •The past, the present and the future •Window Functions • Why do we need window functions? • Syntax • Performance • Why almost complete? •Q&A 5 |© 2012 SolidQ
  • 6. Window Functions •Window function • function applied to a set of rows defined by a window descriptor and returns a single value for each row from the underlying query •Window descriptor • Define which rows will be used with the function © 2012 SolidQ
  • 7. Objectives of this session •Introduction •The past, the present and the future •Window Functions • Why do we need window functions? • Syntax • Performance • Why almost complete? •Q&A 7 |© 2012 SolidQ
  • 8. The past, the present and the future •The SQL 2000 way • SQL Server 2000 does not provide any syntax to support windowing functions •The SQL 2005 way • SQL Server 2005 to 2008R2 introduced a partial implementation of the windowing functions using the OVER clause •The SQL 2012 way • SQL Server 2012 will have an almost complete implementation of the windowing functions © 2012 SolidQ
  • 9. The SQL 2005-2008R2 way •SQL Server 2005 introduces • OVER clause (partially implemented) • New window functions o ROW_NUMBER() o RANK() o DENSE_RANK() o NTILE() •SQL Server 2008/2008R2 added no new implementations in this particular area © 2012 SolidQ
  • 10. The SQL 2005-2008R2 way •OVER clause •Two different implementations depending on type of windowing • Aggregate window functions does not provide the ORDER BY clause © 2012 SolidQ
  • 11. The SQL 2012 way • SQL Server 2012 are getting close to the full implementation of Windowing functions • SQL Server 2012 introduces: • OVER clause almost complete o Order by o Window Frame • 8 new window functions o LAG(), LEAD() o FIRST_VALUE(), LAST_VALUE() o CUME_DIST() o PERCENT_RANK() o PERCENTILE_DISC(), PERCENTILE_COUNT() © 2012 SolidQ
  • 12. OVER clause syntax •SQL Server 2005/2008/R2 • SQL Server 2012 © 2012 SolidQ
  • 13. Objectives of this session •Introduction •The past, the present and the future •Window Functions • Why do we need window functions? • Syntax • Performance • Why almost complete? •Q&A 13 |© 2012 SolidQ
  • 14. Why do we need windows functions? • Why do we need windows functions? • What if we want to get the sum and the value column? (group and detail) • If we are in SQL 2005/2008/2008R2…am i correct with this approach? select id_table, value, sum(value) as [sum(value)] from table1 group by id_table Msg 8120, Level 16, State 1, Line 1 Column 'table1.value' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. id_table value 1 1 2 1 2 2 3 1 3 2 3 3 SUM(value) 1 3 6 id_table value sum(value) 1 1 1 2 1 3 2 2 3 3 1 6 3 2 6 3 3 6 Select sum(value) as [sum(value)] from table1 group by id_table © 2012 SolidQ
  • 15. Why do we need windows functions? •Now we know that the OVER clause exists… •This must be our solution select id_table, value, sum(value) over(partition by id_table) from table1 id_table value sum(value) 1 1 1 2 1 3 2 2 3 3 1 6 3 2 6 3 3 6 © 2012 SolidQ
  • 16. New syntax to solve old problems • Partitioning • Ordering • Slicing/framing © 2012 SolidQ
  • 17. Key concepts: All together Partition UNBOUNDED FOLLOWING UNBOUNDED PRECEDING CURRENT ROW © 2012 SolidQ
  • 19. Key concepts: Partition •Partition is like a group of rows with similar “characteristics” inside a set © 2012 SolidQ
  • 20. Key concepts: Partition •Partition is like a group of rows with similar “characteristics” inside a set © 2012 SolidQ
  • 21. Key concepts: Slicing/Framing • RANGE/ROWS • ROWS | RANGE BETWEEN <B1> AND <B2> • ROWS | RANGE <B1> © 2012 SolidQ
  • 22. Key concepts: Slicing/Framing • B1 and B2 can be • UNBOUNDED PRECEDING • UNBOUNDED FOLLOWING • CURRENT ROW • For “ROWS” clause only o <scalar expression> PRECEDING o <sclara expression> FOLLOWING • Note • B1 <= B2 or NULL will be returned o Except in COUNT() that 0 will be returned © 2012 SolidQ
  • 23. Key concepts: All together Partition UNBOUNDED FOLLOWING UNBOUNDED PRECEDING CURRENT ROW © 2012 SolidQ
  • 24. New windowing functions • New analytic functions in SQL 2012 • offset • LAG() • LEAD() • FIRST_VALUE() • LAST_VALUE() • Distribution • PERCENT_RANK() • CUME_DIST() • PERCENTILE_CONT() • PERCENTILE_DIST() © 2012 SolidQ
  • 25. DEMO 1: NEW FUNCTIONS AND SYNTAX Basics on framing, RANGE vs ROWS, new window functions © 2012 SolidQ
  • 26. Objectives of this session •Introduction •The past, the present and the future •Window Functions • Why do we need window functions? • Syntax • Performance • Why almost complete? •Q&A 26 |© 2012 SolidQ
  • 27. Objectives of this session •Introduction •The past, the present and the future •Window Functions • Why do we need window functions? • Syntax • Performance • Why almost complete? •Q&A 27 |© 2012 SolidQ
  • 28. Performance: IN-MEMORY vs ON DISK • Directly affected by Window Spool Operator • Used to store the framing data • In-memory vs disk-based worktable • In-memory worktable • Fastest • Prerequisites: Framing defined using ROWS and working with <10k rows • Disk-based worktable • Default • Used when the window was defined using RANGE • Used when the windows was defined using ROWS and the frame has more than 10k rows © 2012 SolidQ
  • 29. Performance: Indexing •Index order by the window partitioning elements and then by the order elements •Include the rest of the columns •Optimized execution operators: 1. Scan 2. Segment 3. Window spool 4. Aggregate © 2012 SolidQ © 2012 SolidQ
  • 30. DEMO 2: PERFORMANCE Running Totals, in memory vs on disk, … © 2012 SolidQ
  • 31. Why almost complete? • RANGE only supports • UNBOUNDED PRECEDING • UNBOUNDED FOLLOWING • CURRENT ROW • Ranking functions does not support framing: • ROW_NUMBER, RANK, NTILE,DENSE_RANK, PERCENTILE_DIST, PERCENTILE_CONT • Not implemented • WINDOW aliases • Clauses: NULLs FIRST and NULLs LAST © 2012 SolidQ
  • 32. Objectives of this session •Introduction •The past, the present and the future •Window Functions • Why do we need window functions? • Syntax • Performance • Why almost complete? •Q&A 32 |
  • 33. THANK YOU! Enrique Catalá Bañuls Mentor, SolidQ MAP 2012 – Microsoft Technical Ranger – Microsoft Certified Trainer ecatala@solidq.com Twitter: @enriquecatala