Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Windowing Ranges


Windowing Ranges

Author
Message
Thomas Abraham
Thomas Abraham
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2335 Visits: 2254
Mighty (12/9/2013)
Maybe worthwhile to mention that this functionality was introduced in SQL Server 2012.


Yes! Had a heck of a time figuring it out, UNTIL I realized it must be something new for 2012. Then all became clear.

Thanks for the question.

Please don't go. The drones need you. They look up to you.
Connect to me on LinkedIn
Revenant
Revenant
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5803 Visits: 4718
Straightforward and nice - thanks, Steve!
Ken Wymore
Ken Wymore
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4426 Visits: 2342
Nice question Steve! I wish we would upgrade to 2012 around here so we could use these new functions.

Good stuff.

Ken
Marcia J
Marcia J
SSC Eights!
SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)

Group: General Forum Members
Points: 889 Visits: 1889
Any examples of how these would be used?
WayneS
WayneS
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6235 Visits: 10403
KWymore (12/9/2013)
Nice question Steve! I wish we would upgrade to 2012 around here so we could use these new functions.

Good stuff.

Ken


The upgrades to the OVER clause alone are, IMHO, worth the upgrade.

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8329 Visits: 11580
marcia.j.wilson (12/9/2013)
Any examples of how these would be used?

Running totals. Traditionally done with slow set-based code, with a (relatively!) fast cursor, or with some dangerous unsupported side effect of a language feature.

In SQL Server 2012, very simple:

SELECT   SalesPersonName,
SaleDate,
Amount,
SUM(Amount) OVER (PARTITION BY SalesPersonName
ORDER BY SaleDate
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningTotal
FROM YourTable
ORDER BY SalesPersonName, SaleDate;




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Marcia J
Marcia J
SSC Eights!
SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)

Group: General Forum Members
Points: 889 Visits: 1889
Hugo Kornelis (12/9/2013)
marcia.j.wilson (12/9/2013)
Any examples of how these would be used?

Running totals. Traditionally done with slow set-based code, with a (relatively!) fast cursor, or with some dangerous unsupported side effect of a language feature.

In SQL Server 2012, very simple:

SELECT   SalesPersonName,
SaleDate,
Amount,
SUM(Amount) OVER (PARTITION BY SalesPersonName
ORDER BY SaleDate
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningTotal
FROM YourTable
ORDER BY SalesPersonName, SaleDate;



Thanks. That helped me get a better picture of why you would use this.
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21089 Visits: 18259
WayneS (12/9/2013)
KWymore (12/9/2013)
Nice question Steve! I wish we would upgrade to 2012 around here so we could use these new functions.

Good stuff.

Ken


The upgrades to the OVER clause alone are, IMHO, worth the upgrade.


+1



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10306 Visits: 9591
Love the access to other rows in the set. Still waiting for 2012. Sigh.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Ken Wymore
Ken Wymore
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4426 Visits: 2342
Ed Wagner (12/10/2013)
Love the access to other rows in the set. Still waiting for 2012. Sigh.


I was doing some BI work in QlikView a couple years ago and they had some great built-in analytic functions for lead, lag, percent_rank and a few others. Saved me a bunch of coding. Since that application did everything in memory, access to other rows in the set was a built in feature. Very handy.

Supposedly upgrading to 2012 next year. We shall see...:-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search