Home » SQL & PL/SQL » SQL & PL/SQL » SQL: How to cumulatively sum up values till a condition is satisfied?
SQL: How to cumulatively sum up values till a condition is satisfied? [message #576703] Thu, 07 February 2013 07:26 Go to next message
sammydude
Messages: 34
Registered: November 2007
Member
Hello all,

I need to write a SQL to fulfill a particular requirement.
For each row, the values in Field2 should be cumulatively summed up to see when it will be equal to the value of Field1
Eg. In the following example, Field1 has 15. Field2 values will be summed up cumulatively till they reach 15 (03-JAN-2013)
For the next record Field1 is 8, so Field2 can match 8 on 05-JAN-13.
Day             Field1   Field2
01-JAN-2013	15	10	
02-JAN-2013	8	4
03-JAN-2013	9	1
04-JAN-2013	12	2
05-JAN-2013	7	2

Can this be achieved through SQL?
Using Analytical Functions?

Please help.

Regards,
Sam

[EDITED by LF: applied [code] tags to preserve formatting]

[Updated on: Thu, 07 February 2013 14:02] by Moderator

Report message to a moderator

Re: SQL: How to cumulatively sum up values till a condition is satisfied? [message #576705 is a reply to message #576703] Thu, 07 February 2013 07:32 Go to previous messageGo to next message
cookiemonster
Messages: 10573
Registered: September 2008
Location: Rainy Manchester
Senior Member
Since you appear to be using the same rows in multiple summations I really don't understand what you expect the output to be.
Re: SQL: How to cumulatively sum up values till a condition is satisfied? [message #576706 is a reply to message #576705] Thu, 07 February 2013 07:34 Go to previous messageGo to next message
sammydude
Messages: 34
Registered: November 2007
Member
I would probably need the difference in days between the current row's date and the date on which Field1 value will be met.

Thanks,
Sam
Re: SQL: How to cumulatively sum up values till a condition is satisfied? [message #576707 is a reply to message #576706] Thu, 07 February 2013 07:35 Go to previous messageGo to next message
BlackSwan
Messages: 21940
Registered: January 2009
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: SQL: How to cumulatively sum up values till a condition is satisfied? [message #576713 is a reply to message #576703] Thu, 07 February 2013 07:43 Go to previous messageGo to next message
Michel Cadot
Messages: 57606
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" or "Preview Quick Reply" button to verify.
Also always post your Oracle version, with 4 decimals.

With any SQL or PL/SQL question, please, Post a working Test case: create table and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Regards
Michel
Re: SQL: How to cumulatively sum up values till a condition is satisfied? [message #576842 is a reply to message #576706] Fri, 08 February 2013 09:13 Go to previous message
Solomon Yakobson
Messages: 1791
Registered: January 2010
Senior Member
What about this?

SY.
Previous Topic: How to use the column names generated from Dynamic SQL
Next Topic: Char to Date Conversion
Goto Forum:
  


Current Time: Thu Apr 17 13:57:02 CDT 2014

Total time taken to generate the page: 0.09101 seconds