• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Running Totals in an SQL Query

    March 2, 2011 Skip Marchesani

    There are situations when running an SQL query for data analysis where I like to generate a running total based on a column in the table that is the target of the query, to assist with the data analysis.

    “What’s a running total?” you ask. Wikipedia and I define it as the summation of a sequence of numbers that is updated each time a new number is added to the sequence, simply by adding the value of the new number to the running total.

    To explain how to generate a running total, I use an example based on a department master table, Dept_Master, with the following format.

    Column Name	Description
    Dpt_Nbr		Department Number
    Dpt_Name		Department Name
    Emp_Count		Department Employee Count
    

    For the purposes of our discussion, the Dept_Master table has the following rows, listed in arrival sequence for the table.

    Dpt_Nbr	Dpt_Name		Emp_Count
    901	Accounting	3
    911	Sales		11
    977	Manufact		27
    990	Maint		7
    

    For the example, I generate a running total based on the employee count for each department, and see the result set in descending sequence based on the employee count as shown below.

    Dpt_Nbr	Dpt_Name		Emp_Count	   Running_Total
    977	Manufact		27	   27
    911	Sales		11	   38
    990	Maint		7	   45
    901	Accounting	3	   48
    

    To generate the running total, the employee count column for each row has to be added to the running total column from the preceding row, while processing the rows in the desired sequence. In this example the rows will be processed in descending sequence based on employee count.

    The first row to be processed is department number 977 with an employee count of 27. Since this is the first row in the sequence, there is no preceding row and the running total has an initial value of zero. Therefore the running total for department number 977 is 27 (0 + 27 = 27).

    The second row of the result set is department number 911. Since it has an employee count of 11, the running total is 38. This is the sum of the running total from the preceding row of the result set (department number 977) and the employee count of department number 911 (27 + 11 = 38). The third row in the result set is department number 990 and will have a running total of 45 (38 + 7 = 45). The fourth and last row in the result set is department 901 and will have a running total of 48 (45 + 3 = 48).

    Another way to look at this, and the way I look at it from an SQL perspective, is that the running total is the sum of the target column of the current row being processed and all those rows that precede the current row, processing the rows in the desired sequence. Since in this example we are calculating the running total based on the employee count in descending sequence, the running total for department number 977 is calculated as 0 + 27 = 27 (zero since no row preceded it), department 911 is calculated as 27 + 11 = 38, department 990 is calculated as 27 + 11 + 7 = 45, and department 901 is calculated as 27 + 11 + 7 + 3 = 48.

    The question is how to format an SQL SELECT statement to do the above. The answer is to use a scalar subselect in the column list of the SELECT statement to calculate or derive the running total for each row as shown below.

    SELECT dpt_nbr,  dpt_name,  emp_count,
               		    (SELECT SUM(emp_count)
                        		FROM  dept_master  b
                        		WHERE  b.emp_count  >=  a.emp_count)
                   	        AS  running_total
       	    FROM  dept_master  a 
       	    ORDER BY  emp_count  DESC;
    

    The scalar subselect is the SELECT statement enclosed in parentheses immediately following “emp_count” in the column list for the SELECT. The AS operator that follows the scalar subselect assigns the name “running_total” to the calculated or derived column. When executed this SQL syntax returns the desired result set.

    Dpt_Nbr	Dpt_Name		Emp_Count	   Running_Total
    977	Manufact		27	   27
    911	Sales		11	   38
    990	Maint		7	   45
    901	Accounting	3	   48
    

    The scalar subselect derives the running total for the row being processed by summing the employee count for any row where the employee count (b.emp_count) is greater than or equal to the employee count for the current row (a.emp_count).

        		    (SELECT SUM(emp_count)
                        		FROM  dept_master  b
                        		WHERE  b.emp_count  >=  a.emp_count)
                   	        AS  running_total
    

    In this example the result set with the running total is based on employee count in descending sequence. How would this syntax be changed if we wanted the sequence to be changed to ascending sequence? The answer is simple. The greater than or equal to argument (>=) in the WHERE clause for the scalar subselect is changed to a less than or equal argument (<=).

    The syntax for the SELECT statement then becomes:

    SELECT dpt_nbr,  dpt_name,  emp_count,
               		    (SELECT SUM(emp_count)
                        		FROM  dept_master  b
                        		WHERE  b.emp_count  <=  a.emp_count)
                   	        AS  running_total
       	    FROM  dept_master  a
       	    ORDER BY  emp_count  DESC
    

    And the result set is:

    Dpt_Nbr	Dpt_Name		Emp_Count	   Running_Total
    901	Accounting	3	   3
    990	Maint		7	   10
    911	Sales		11	   21
    977	Manufact		27	   48
    

    There are a couple of things to understand about running totals to get them to work correctly in an SQL Query.

    Results will be unpredictable and not correct if:

    1. The column name(s) used in the WHERE clause for the scalar subselect are not the same as those used in the ORDER by clause for the outer or primary SELECT statement. If the names are different, the data contained in the columns must be based on the same thing. In other words don’t mix apples and oranges in the WHERE clause for the scalar subselect and the ORDER BY clause for the outer or primary SELECT statement. For example you can’t use “emp_count” in the WHERE clause and “dpt_name” in the ORDER BY clause.

    2. The argument of the WHERE clause must match the sequence in the ORDER BY clause. This means a WHERE clause argument of <= (less than or equal) is used in conjunction with an ascending sequence in the ORDER BY clause (ascending is the default sequence for the ORDER BY clause), and a WHERE clause of >= (greater than or equal) is used in conjunction with a descending sequence in the ORDER BY clause (ORDER BY xxxx DESC).

    This technique works very nicely and is easy to use when the target of the running total is an existing column in a table or view, as opposed to a derived column. If you should choose to try and use it in conjunction with a derived column, the complexity of the syntax for the scalar subselect increases with and is proportional to the complexity of the expression that is used to for the column derivation. Speaking from experience, when using a derived column, the syntax for the scalar subselect becomes very complex very quickly.

    Understanding these considerations is the key to putting running totals to work for you.

    Skip Marchesani retired from IBM after 30 years and is now a consultant with Custom Systems Corporation. He is also a founding partner of System i Developer and the RPG & DB2 Summit. Skip spent much of his IBM career working with the Rochester Development Lab on projects for S/38 and AS/400 and was involved with the development of the AS/400. He was part of the team that taught early AS/400 education to customers and IBM lab sites worldwide. Skip is recognized as an industry expert on DB2 for i and the author of the book DB2/400: The New AS/400 Database. He specializes in providing customized education for any area of the System i, iSeries, and AS/400; does database design and design reviews; and performs general System i, iSeries, and AS/400 consulting for interested clients. He has been a speaker for user groups, technical conferences, and System i, iSeries, and AS/400 audiences around the world. He is an award-winning COMMON speaker and has received its Distinguished Service Award. Send your questions or comments for Skip to Ted Holt via the IT Jungle Contact page.



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    DRV Tech

    Get More Out of Your IBM i

    With soaring costs, operational data is more critical than ever. IBM shops need faster, easier ways to distribute IBM applications-based data to users more efficiently, no matter where they are.

    The Problem:

    For Users, IBM Data Can Be Difficult to Get To

    IBM Applications generate reports as spooled files, originally designed to be printed. Often those reports are packed together with so much data it makes them difficult to read. Add to that hardcopy is a pain to distribute. User-friendly formats like Excel and PDF are better, offering sorting, searching, and easy portability but getting IBM reports into these formats can be tricky without the right tools.

    The Solution:

    IBM i Reports can easily be converted to easy to read and share formats like Excel and PDF and Delivered by Email

    Converting IBM i, iSeries, and AS400 reports into Excel and PDF is now a lot easier with SpoolFlex software by DRV Tech.  If you or your users are still doing this manually, think how much time is wasted dragging and reformatting to make a report readable. How much time would be saved if they were automatically formatted correctly and delivered to one or multiple recipients.

    SpoolFlex converts spooled files to Excel and PDF, automatically emailing them, and saving copies to network shared folders. SpoolFlex converts complex reports to Excel, removing unwanted headers, splitting large reports out for individual recipients, and delivering to users whether they are at the office or working from home.

    Watch our 2-minute video and see DRV’s powerful SpoolFlex software can solve your file conversion challenges.

    Watch Video

    DRV Tech

    www.drvtech.com

    866.378.3366

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    System i Developer:  Upgrade your skills at the RPG & DB2 Summit in Orlando, March 22-24
    Townsend Security:  Learn how to easily and securely communicate with XML
    Northeast User Groups Conference:  21th Annual Conference, April 11 - 13, Framingham, MA

    IT Jungle Store Top Book Picks

    BACK IN STOCK: Easy Steps to Internet Programming for System i: List Price, $49.95

    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The iSeries Pocket Database Guide: List Price, $59
    The iSeries Pocket SQL Guide: List Price, $59
    The iSeries Pocket WebFacing Primer: List Price, $39
    Migrating to WebSphere Express for iSeries: List Price, $49
    Getting Started with WebSphere Express for iSeries: List Price, $49
    The All-Everything Operating System: List Price, $35
    The Best Joomla! Tutorial Ever!: List Price, $19.95

    Solarsoft Reports New Implementations of iVP Suite Some Insight Into the iASP and ISV Issue

    Leave a Reply Cancel reply

Volume 11, Number 8 -- March 2, 2011
THIS ISSUE SPONSORED BY:

WorksRight Software
SEQUEL Software
Northeast User Groups Conference

Table of Contents

  • Running Totals in an SQL Query
  • Odds and Ends: The Reader is the Guru
  • Admin Alert: Corralling i/OS Storage Hogs, Part 1

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle