You are on page 1of 32

DBI-B333

T-SQL Power!
SQL Server Windows
That Open Doors
Adam Machanic
@AdamMachanic
Data Education

Adam Machanic
SQL
Architect
Boston, MA
Financial
industry

Writer
Books,
magazines,
web sites

amachanic@gmail.com

Speaker
PASS,
TechEd,
Connections

Founder
SQLBlog.co
m

@AdamMachanic

Agenda
Background

Lots of
Demos

(Thats it.)

Windowing and Framing


Frames as many as you
need

101011011010101010111001010010001010100101001001001000

Framing Rows
ORDER BY ProductId,
TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Framing Rows
ORDER BY ProductId,
TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Framing Rows
ORDER BY ProductId,
TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Framing Rows
ORDER BY ProductId,
TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Framing Rows
ORDER BY ProductId,
TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Framing Rows
ORDER BY ProductId,
TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Framing Rows
ORDER BY ProductId,
TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Framing Rows
ORDER BY ProductId,
TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Partitioning and Framing Rows


PARTITION BY ProductId
ORDER BY TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Partitioning and Framing Rows


PARTITION BY ProductId
ORDER BY TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Partitioning and Framing Rows


PARTITION BY ProductId
ORDER BY TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Partitioning and Framing Rows


PARTITION BY ProductId
ORDER BY TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Partitioning and Framing Rows


PARTITION BY ProductId
ORDER BY TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Partitioning and Framing Rows


PARTITION BY ProductId
ORDER BY TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Partitioning and Framing Rows


PARTITION BY ProductId
ORDER BY TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Partitioning and Framing Rows


PARTITION BY ProductId
ORDER BY TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Partitioning and Framing Rows


PARTITION BY ProductId
ORDER BY TransactionDate

ROWS BETWEEN
1 PRECEDING
AND CURRENT ROW
ROWS BETWEEN
3 PRECEDING
AND 2 FOLLOWING

Commonly Used Window Function


Types
Ranking
ROW_NUMBER
RANK
DENSE_RANK

Analytic
Offset
LAG
LEAD

Analytic
Framed
FIRST_VALUE
LAST_VALUE

Aggregate
SUM
AVG
COUNT
MIN
MAX

Query Processor Iterators of


Interest

demo
Windows That Open Doors

Summary
Window
functions
make
complex TSQL much
easier

Performance,
on the other
hand, is up to
you

Beware of
RANGE mode

Be creative!
There are
endless use
cases for
these
features

Related content

Required
Slide

*delete this box when your slide is


finalized

Breakout Sessions (session codes and


titles)
Speakers, please list
Labs (session codes and titles)

the Breakout Sessions,


Labs, Microsoft
Solutions Experience
Location and
Certification Exams
that relate to your
session. Also indicate
when they can find
you staffing in the
MSE.

Microsoft Solutions Experience Location (MSE)


Related Certification Exam
Find Me Later At. . .

Track resources
Download Microsoft SQL Server 2014
http://www.trySQLSever.com

Try out Power BI for Office 365!


http://www.powerbi.com

Sign up for Microsoft HDInsight today!


http://microsoft.com/bigdata

Resources
Learning
Sessions on Demand

http://channel9.msdn.com/Events/Tec
hEd

TechNet
Resources for IT Professionals

http://microsoft.com/technet

Microsoft Certification & Training Resources

www.microsoft.com/learning

msdn
Resources for Developers

http://microsoft.com/msdn

Complete an evaluation and enter


to win!

Evaluate this session


Scan this
QR code
to evaluate
this session.

2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be
interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR
STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

You might also like