Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

sql query Expand / Collapse
Author
Message
Posted Monday, September 2, 2013 8:47 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:03 PM
Points: 3,609, Visits: 5,221
Jeff Moden (9/2/2013)
dwain.c (9/2/2013)
Jeff Moden (9/2/2013)
Same goes for another atypical method (the "Multi-Pass" update) that a chap by the name of "Hugo" wrote.


My curiosity has been piqued. Would this be Hugo Kornelski and where would this atypical method be elaborated?


No. Different Hugo... Hugo Kornelis.

Here's his original post on the subject...
http://www.sqlservercentral.com/Forums/FindPost816917.aspx

Here's the post where I made a suggestion that cut about a 1/3rd out of the duration (same thread)...
http://www.sqlservercentral.com/Forums/FindPost816964.aspx


Hah! Same Hugo but I forgot how to spell his name!

Thanks Jeff!



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1490732
Posted Tuesday, September 3, 2013 12:44 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 5:18 PM
Points: 42,437, Visits: 35,492
iirc he also wrote a chapter in one of the MVP Deep Dives on the subject.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1490765
Posted Tuesday, September 3, 2013 1:45 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Today @ 2:28 PM
Points: 786, Visits: 691
Jeff Moden (9/2/2013)
I guess "possible" depends a lot on whether or not you consider the "Quirky Update" to be a form of ordered aggregate or not


If by "quirky update" you mean that you use the UPDATE statement and you get a desired solution when there are certain indexes in place, stars are aligned, I don't consider that to be an ordered aggregate or an acceptable solution at all for that matter.

As reasonable as it sounds, though, I would't assume that any given class has been updated to the latest version epecially when the OP posted in a 2008 forum.


The instructor that does not accept an ANSI-compatible solution that runs on the latest version is grossly incompetent.

That said, a good instructor should encourage his students to try different solutions, since not all platforms support ordered aggregates. There is certainly all reason to teach the solution with the correlated subquery, despite its dreadful performance. After all, the purpose of teaching SQL is not only to get the students to learn specific patterns, but also to learn the building blocks, and to that end the correlated subquery for running sums is an excellent exercise.


Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Post #1490791
Posted Tuesday, September 3, 2013 4:56 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:57 AM
Points: 7,115, Visits: 13,476
Jeff Moden (9/2/2013)
ramya.usapp (9/1/2013)
Hello friends,

Please help me in writing a query!!

Question:

Write a query based off of the following data stored in the AccountingEntry table. The query needs to return running totals and should produce the exact results for each customer listed below.

Deposits – amounts added to an account
Charge – amounts should be subtracted from a deposit
Cancel/Refund – amount returned to a customer, should be subtracted from a deposit

AccountingEntryID CustomerID Date Type Amount
1 1 1/1/2013 Deposit 400
2 3 1/23/2013 Deposit 900
3 19 2/28/2013 Deposit 250
4 23 3/15/2013 Charge 175
5 1 2/1/2013 Charge 350
6 15 4/1/2013 Deposit 2000
7 3 2/23/2013 Charge 500
8 15 4/1/2013 Charge 100
9 1 2/23/2013 Deposit 100
10 23 3/15/2013 Charge 175
11 15 4/1/2013 Charge 750
12 1 2/15/2013 Charge 25
13 15 4/1/2013 Cancel/Refund 1150
14 3 1/25/2013 Deposit 100
15 15 4/1/2013 Deposit 750
16 19 3/28/2013 Charge 100
17 15 4/1/2013 Deposit 100
18 3 3/23/2013 Charge 500
19 23 3/15/2013 Deposit 400
20 19 4/28/2013 Charge 100
21 1 3/15/2013 Cancel/Refund 125

Results should look like

AccountingEntryID CustomerID Date Type Amount Running Deposit RunningAmount Charged RunningAmt Remaining
1 1 1/1/2013 Deposit 400 400 0 400
5 1 2/1/2013 Charge 350 400 350 50
12 1 2/15/2013 Charge 25 400 375 25
9 1 2/23/2013 Deposit 100 500 375 125
21 1 3/15/2013 Cancel/Refund 125 500 375 0


Thanks,
RT



Since this is homework, you must have an instructor. There are 3 typical ways of solving this problem in SQL Server 2008... Cursor/While Loop, Temp Table/While Loop, Correlated Sub-Query (which would form a "Triangular Join" which is very bad but the instructor may be teaching it).

There' also an atypical method known as the "Quirky Update" which will blow all 3 of those methods out of the water but it's not likely that your instructor taught it or even knows anything about it. Same goes for another atypical method (the "Multi-Pass" update) that a chap by the name of "Hugo" wrote.

With all that in mind, my question to you is what has your instructor recently covered out of the 5 things mentioned above so that we can play into what the instructor actually wants to see?

Also, if you want much better help, please see the first "Helpful Link" in my signature line below.


Hey Jeff! Don't forget the good ol' rCTE!


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1490879
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse