SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Server's implementation of Window Functions


SQL Server's implementation of Window Functions

Author
Message
WayneS
WayneS
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: 21230 Visits: 10652
I'm not sure that this would fall into the "wide range" that you're looking for Steve, but here's my take on these:
Ordering for aggregates (used to allow subsequent framing options)
Framing (ROWS and RANGE window sub-clauses)

These two will allow one to perform "running totals", in a SUPPORTED, SET-BASED way. Now, running totals are frequently coming up here, and I definitely see these two as very highly desired. I personally would be happy to see just these two fully implemented to where running totals can be performed.


The other one that is highly desirable to me is:
Reuse of window definition using the WINDOW clause

This is just code re-use - it allows me to use the same windowing definition in multiple areas without having to redefine it everywhere. And if something needs changed, it only needs changed once instead of everywhere it's being used.

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

Phil Factor
Phil Factor
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4780 Visits: 3031
I have yet to see people give me a good, wide range of where these things help in an every day situation.


Window functions are great for any 'inter-row' calculations where you need to refer to values in adjacent or nearly adjacent rows based on the order you define. They are the easy way to calculate moving averages. They are usually the best way of paging through results in the order you define (e.g. getting row 30 to 40, row 40 to 50 etc.) cumulative totals as already mentioned, calculating rankings within sets of data, calculating relative contributions to a total. Sots of statistical stuff as well, but I know you're not into that, Steve!


Best wishes,

Phil Factor
Simple Talk
Steve Jones
Steve Jones
SSC Guru
SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)

Group: Administrators
Points: 143904 Visits: 19424
Wayne/Phil,

Those are good examples, but how often do you write them? It seems that you "rarely" solve these issues, get the queries, and often do other things.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
ganotedp
ganotedp
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 216
I have yet to see people give me a good, wide range of where these things help in an every day situation.

Depends on your "every day". If it's OLTP, well, that's been a "known quantity" for a long time and Windowing Functions would be merely a curiosity item.

But Windowing Functions, I hear, were originally promoted by Teradata, which gives you a clue where they're valuable.

If your "every day" is OLAP, then Windowing Functions, partitioning, bitmap indexing, compression, and such keep you from being swamped by ever-growing data sizes and query execution times. OLAP is where most new DBMS features are aimed, in my opinion.

I use Windowing Functions every day in my ETL. I just used them in this query to find some duplicates and identify the preferred ID to replace them with:

select * from (
select ROW_NUMBER() OVER (partition by policy_id, unit_Number order by unit_id desc ) r#
, MAX(unit_ID) over (partition by policy_id, unit_Number ) preferred_unit_id
, m.* from unit_table m
) where r# > 1



The FIRST_VALUE Windowing Function is a real blessing, and I know I'd miss it if I had to switch to SS (I even have a certification from Oracle that calls me an SQL Expert, so I know I could "code" around the gap, but it'd be slower to both write and to run).

I've thoroughly converted all of my ETL team into "true believers" in the value of Windowing (Analytic) Functions. As Tom Kyte has said: 11_QUESTION_ID:12864646978683" target="_blank" class="SmlLinks">Analytics rock! Cool
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