How to get Running Total in Oracle

No.of Views1842
Bookmarked1 times
Downloads 
Votes0
By  youngmurukan   On  15 Feb 2010 21:02:15
Tag : Oracle , PL/SQL
How to get Running Total in Oracle
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

This very frequent needful thing for developers as they need to create so many reports based on this concept.
For this you will have to use one of the window functions in oracle which is ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

This would add each and every value with previous value and give like Running Total.
Query for this,

{codecitation class="brush: sql; gutter: true;" width="650px"}


SELECT PRODUCT_NO,PL_NO,  UNRESTRICTED_QTY,
SUM(UNRESTRICTED_QTY) OVER (ORDER BY PRODUCT_NO ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) RUNNING_TOTAL
FROM PRODUCT_LOCATION
WHERE CLIENT_C=UPPER('MSWG') AND UNRESTRICTED_QTY>0 AND     LOCATION_NO='RECEIPT_BAY'


{/codecitation}

runingtotalimg02

Explanation


In this query, UNBOUNDED PRECEDING means start of the window is fixed at first row in the result set and adding up the values with currently reading value which was specified by CURRENT ROW up to last record of the record set.
And ordering results by PRODUCT_NO
The result of the above query has shown as below.

Result

Image loading...

I hope you ae like it and enjoy with happy PL/SQL

Thank you

Murukan

 
Sign Up to vote for this article
 
About Author
 
youngmurukan
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-12 May 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top