• 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:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags:

    Sponsored by
    Manta Technologies

    The Leader in IBM i Education!
    Need training on anything i?
    Manta is all you need.

    Courses and competency exams on:
    · IBM i operations
    · System Management and Security
    · IBM i Programming Tools
    · Programming in RPG, COBOL, CL, Java
    · Web Development

    SQL, DB2, QueryProduct features:
    · Runs in every popular browser
    · Available 24/7/365
    · Free Student Reference Guides
    · Free Student Administration
    · Concurrent User License
    · Built-In IBM i Simulator

    You can download our 200-page catalog and take sample sessions at MantaTech.com

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) 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 Security Policies Vs. Security Procedures

    Leave a ReplyCancel 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

  • Power Systems Turns In Another Great Quarter
  • Big Blue Is Right On Schedule With Fall 2026 IBM i Tech Refreshes
  • Guru: Environment Variables – Global Variables Wearing Hiking Boots
  • A New IBM i Entity Takes Shape: Power By Fortra
  • IBM i PTF Guide, Volume 28, Number 38
  • Critical PHP2Shell Security Vulns Evoke ‘Black October’
  • Why Fortra Partnered with FalconStor to Launch Continuity Care: A Validated Recovery Service for IBM i
  • Guru: Analyzing User Session Statistics, Part 2
  • Fresche Launches OpsIQ, a Free AI-Based Operation Assistant for IBM i
  • IBM i PTF Guide, Volume 28, Number 37

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