• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Two Ways To Rollup

    June 16, 2015 Ted Holt

    In Three Powerful SQL Words, I showed how to enhance summary queries with aggregate values over subsets of grouping columns. Today I continue that discussion by showing the two syntaxes of rollup and illustrating the difference between them.

    First, assume a table of accounting transactions.

    select department, account, amount
      from xacts2
     order by 1, 2
     
    DEPARTMENT  ACCOUNT     AMOUNT
         1          10      200.00
         1          10      250.00
         1          30      300.00
         2          10      125.00
         2          20      175.00
         2          20      225.00
    

    Here’s a typical summary query using the familiar GROUP BY clause.

    select department, account, sum(amount) as tamt
      from xacts2                                  
     group by department, account                  
     order by 1,2                                  
    
    DEPARTMENT  ACCOUNT       TAMT
         1          10      450.00
         1          30      300.00
         2          10      125.00
         2          20      400.00
    

    The amount column is summarized by two columns–department and account.

    To add additional aggregate totals is an easy matter of adding WITH ROLLUP.

    select department, account, sum(amount) as tamt
      from xacts2                                  
     group by department, account                  
      with rollup 
     order by 1,2  
     
    DEPARTMENT  ACCOUNT         TAMT
         1          10        450.00
         1          30        300.00
         1            -       750.00
         2          10        125.00
         2          20        400.00
         2            -       525.00
          -           -     1,275.00
    

    This gives me department totals and grand totals.

    Here’s a different form of rollup that does the same thing.

    select department, account, sum(amount) as tamt
      from xacts2                                  
     group by rollup(department, account)          
     order by 1,2                                  
    
    DEPARTMENT  ACCOUNT        TAMT
         1          10       450.00
         1          30       300.00
         1            -      750.00
         2          10       125.00
         2          20       400.00
         2            -      525.00
          -           -    1,275.00
    

    So is one form as good as another? Consider the following queries.

    select department, account, sum(amount) as tamt
      from xacts2                                  
     group by rollup(department),account           
     order by 1,2                                  
    
    DEPARTMENT  ACCOUNT       TAMT
         1          10      450.00
         1          30      300.00
         2          10      125.00
         2          20      400.00
          -         10      575.00
          -         20      400.00
          -         30      300.00
    

    Notice that there are aggregates for account, but not for department.

    Here’s another variation.

    select department, account, sum(amount) as tamt
      from xacts2                                  
     group by department ,rollup(account)          
     order by 1,2                                  
    
    DEPARTMENT  ACCOUNT        TAMT
         1          10       450.00
         1          30       300.00
         1            -      750.00
         2          10       125.00
         2          20       400.00
         2            -      525.00
    

    Here we have aggregate figures for department, but not for account.

    Consider one final example.

    select department, account, sum(amount) as tamt
      from xacts2                                  
     group by rollup(department) ,rollup(account)  
     order by 1,2                                  
    	  
    DEPARTMENT  ACCOUNT        TAMT
         1          10       450.00
         1          30       300.00
         1            -      750.00
         2          10       125.00
         2          20       400.00
         2            -      525.00
          -         10       575.00
          -         20       400.00
          -         30       300.00
          -           -    1,275.00
    

    We have totals by department, totals by account, and grand totals. This is equivalent to WITH CUBE.

    Which form is better–GROUP BY ROLLUP or WITH ROLLUP? Probably GROUP BY ROLLUP, for two reasons:

    1. From what I can tell, GROUP BY ROLLUP seems to be more standard and widespread throughout the relational database world than WITH ROLLUP.

    2. The GROUP BY ROLLUP syntax allows us to produce result sets that we can’t get with WITH ROLLUP.

    For these reasons, I plan to devote more time to mastering the nuances of the GROUP BY ROLLUP syntax.

    RELATED STORIES

    Three Powerful SQL Words

    Treasury of new DB2 6.1 Features, Part 2: Grouping Sets and Super Groups

    IBM Knowledge Center

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    HiT Software:  Make your data pay off quickly and easily, regardless of location with DBMoto.
    SystemObjects:  Send SMS, email & push messages from your IBM i applications!
    LaserVault:  FREE ON-DEMAND WEBINAR: Understanding Tapeless Backups. Watch it now >

    OpenPower Partners Open SuperVessel Dev Cloud dbFunnel Provides Native ETL For IBM i

    Leave a Reply Cancel reply

Volume 15, Number 12 -- June 16, 2015
THIS ISSUE SPONSORED BY:

ProData Computer Services
CCSS
WorksRight Software

Table of Contents

  • iSphere Plug-in Expands RSE/RDi Toolset
  • Two Ways To Rollup
  • Security Policies Vs. Security Procedures

Content archive

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

Recent Posts

  • The Power11 Transistor Count Discrepancies Explained – Sort Of
  • Is Your IBM i HA/DR Actually Tested – Or Just Installed?
  • Big Blue Delivers IBM i Customer Requests In ACS Update
  • New DbToo SDK Hooks RPG And Db2 For i To External Services
  • IBM i PTF Guide, Volume 27, Number 33
  • Tool Aims To Streamline Git Integration For Old School IBM i Devs
  • IBM To Add Full System Replication And FlashCopy To PowerHA
  • Guru: Decoding Base64 ASCII
  • The Price Tweaking Continues For Power Systems
  • IBM i PTF Guide, Volume 27, Numbers 31 And 32

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