Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase ««12

SQL Server's implementation of Window Functions Expand / Collapse
Posted Tuesday, September 28, 2010 10:12 AM



Group: General Forum Members
Last Login: Yesterday @ 9:27 AM
Points: 5,911, Visits: 10,368
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.

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
Post #994615
Posted Tuesday, September 28, 2010 11:56 AM

SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Today @ 3:53 AM
Points: 659, Visits: 2,921
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
Post #994692
Posted Tuesday, September 28, 2010 4:11 PM



Group: Administrators
Last Login: Yesterday @ 12:17 PM
Points: 34,363, Visits: 18,583

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
Post #994853
Posted Monday, November 8, 2010 9:16 AM



Group: General Forum Members
Last Login: Friday, November 18, 2016 2:38 PM
Points: 19, Visits: 199
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: Analytics rock!
Post #1017258
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse