December 9, 2013 at 2:18 pm
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;
December 9, 2013 at 9:10 pm
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.
December 9, 2013 at 10:59 pm
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[/url]
Learn Extended Events
December 10, 2013 at 5:47 am
Love the access to other rows in the set. Still waiting for 2012. Sigh.
December 10, 2013 at 12:03 pm
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...:-)
January 3, 2014 at 12:49 am
Special dedicace to MSDN for refreshing my memory loss 🙂
January 6, 2014 at 10:04 am
Good Question. Maybe specify database version though as that only became valid in 2012.
January 6, 2014 at 12:11 pm
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.
Unsupported... Yes. Dangerous? No and I do wish you'd get over that. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
March 31, 2015 at 7:18 am
Some say this was an easy one. For me I had to do some research. I hadn't used this with ROWS and RANGE before. I learned something today.
Viewing 9 posts - 16 through 23 (of 23 total)
You must be logged in to reply to this topic. Login to reply