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 Sunday, September 1, 2013 3:15 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 3, 2013 9:11 AM
Points: 5, Visits: 23



Thanks,
RT
Post #1490485
Posted Sunday, September 1, 2013 4:09 PM


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 @ 6:50 AM
Points: 40,425, Visits: 36,875
Homework? What have you tried so far? Where are you stuck?


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 #1490490
Posted Sunday, September 1, 2013 4:17 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 3, 2013 9:11 AM
Points: 5, Visits: 23
Yes it's a home work. I need an idea to start up and solve the query!!!
Post #1490493
Posted Sunday, September 1, 2013 4:26 PM


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 @ 6:50 AM
Points: 40,425, Visits: 36,875
Here's an idea. Try it yourself. If you get stuck, ask for help. You learn nothing by having someone give you the answer.

Hint: Sum and Group By



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 #1490496
Posted Sunday, September 1, 2013 11:14 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:43 AM
Points: 1,945, Visits: 2,386
can you please post the query that you have written so that we can see what you have tried and help you


_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1490509
Posted Monday, September 2, 2013 12:50 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:32 PM
Points: 35,584, Visits: 32,174
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.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1490678
Posted Monday, September 2, 2013 4:14 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 2:39 PM
Points: 816, Visits: 742
Jeff Moden (9/2/2013)
Same goes for another atypical method (the "Multi-Pass" update) that a chap by the name of "Hugo" wrote.


I assume that you mean set-based iteration. That is by no means an atypical method, but one a good SQL programmer should master.

Of course, for this particular problem, the correct solution is an ordered aggregate. No that is not possible on SQL 2008, but it's reasonable to assume that a class would teach from the most recent version. At least if the features are ANSI-compatible.


Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Post #1490703
Posted Monday, September 2, 2013 6:05 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:32 PM
Points: 35,584, Visits: 32,174
Erland Sommarskog (9/2/2013)
I assume that you mean set-based iteration. That is by no means an atypical method, but one a good SQL programmer should master.


Yes and absolutely agreed. That's why I called it atypical though.


Of course, for this particular problem, the correct solution is an ordered aggregate. No that is not possible on SQL 2008, but it's reasonable to assume that a class would teach from the most recent version. At least if the features are ANSI-compatible.


I guess "possible" depends a lot on whether or not you consider the "Quirky Update" to be a form of ordered aggregate or not but I do get your point. 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.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1490716
Posted Monday, September 2, 2013 7:08 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: 2 days ago @ 4:53 AM
Points: 3,422, Visits: 5,368
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?



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 #1490723
Posted Monday, September 2, 2013 8:40 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:32 PM
Points: 35,584, Visits: 32,174
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


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1490731
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse