• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Treasury of new DB2 6.1 Features, Part 2: Grouping Sets and Super Groups

    March 11, 2009 Michael Sansoterra

    Part 1 of this series focused on some of the stellar query enhancements IBM gave its DB2 for i developer community. It’s hard to believe the 20-plus-year-old AS/400 system (now called IBM i) can continue to look so young given its great strengths in database and other areas. But for now, I want to focus on specialized query enhancements called grouping sets and super groups.

    The ability to work with grouping sets and super groups in a query is an overdue feature, in my opinion (which incidentally has long been available with other versions of DB2 and other database servers). This nifty enhancement gives developers a simple way to include summary rows within their query’s result set.

    To illustrate, let’s start with a simple example. Say we have a view called “vwSalesOrderInfo” that contains order summary information:

    ORDERDATE

    TIMESTAMP

     

    SALESPERSONID

    INTEGER

     

    SALES_PERSON

    VARGRAPHIC

    102

    TERRITORYID

    INTEGER

     

    TERRITORY

    VARGRAPHIC

    50

    CUSTOMERID

    INTEGER

     

    CUSTOMER

    VARGRAPHIC

    50

    SUBTOTAL

    DECIMAL

    19,4

    TAXAMT

    DECIMAL

    19,4

    FREIGHT

    DECIMAL

    19,4

    TOTALDUE

    DECIMAL

    19,4

    For clarification, the hierarchy of data is Customers are within a territory, and territories are within the jurisdiction of a sales person. And just for the record, I took this sample data from Microsoft‘s AdventureWorks SQL Server sample database and put it on DB2.

    From a business intelligence (BI) standpoint, we could ask several questions about this data, such as:

    1. What are the company’s total sales for all orders stored in the system?
    2. What is the total order value (excluding tax, freight) for each customer?
    3. What is the total order value (excluding tax, freight) for each territory?
    4. What is the total order value (excluding tax, freight) for each sales rep?
    5. What is the total order value (excluding tax, freight) for each year?
    6. What is the average order value by customer?
    7. Which customers grew in sales from one year to the next?
    8. Which customers dropped in sales from one year to the next?
    9. What are the peak sales months of the year?
    10. What is each sales rep’s average order value?
    11. Which territories incur the largest freight charges?
    12. Which territories incur the largest sales tax collection?

    Of course we could continue to ask many more questions from the data at hand.

    We can easily answer the first question “What are the total sales for all orders?” using the following query:

    Select Sum(SubTotal) As OrderValue From vwSalesOrderInfo
    

    That query will return the value: 127337180.1126

    We can easily answer the second question “what is the total order value (excluding tax, freight) for each customer?” using the following query:

    Select Customer,Sum(SubTotal) As OrderValue
      From vwSalesOrderInfo
    Group By Customer
    Order By Customer
    

    Partial formatted results for this query look like the following:

    Customer

    OrderValue

    A Bike Store

    $
    102,351.80

    A Great Bicycle
    Company

    $ 10,256.08

    A Typical Bike Shop

    $
    100,227.71

    Acceptable Sales
    & Service

    $ 1,510.05

    Accessories Network

    $ 2,711.33

    Acclaimed Bicycle
    Company

    $ 4,735.81

    Ace Bicycle Supply

    $
    4,422.64

    Active Cycling

    $ 1,691.26

    Active Systems

    $ 328.80

    Active Transport
    Inc.

    $
    102,029.76

    Activity Center

    $ 51,534.07

    Advanced Bike
    Components

    $
    188,367.91

     

    We can answer questions 3 through 5 in the same way by simply “changing out” the column to “summarize by” in the SELECT, GROUP BY, and ORDER BY clauses. For instance, instead of summarizing by Customer, we can summarize by territory.

    Now, what if we wanted to combine all of this information and answer all these questions in a single query? Couldn’t we simply expand the query to summarize by all combinations as follows?

      Select Year(OrderDate) As OrderYear,Sales_Person,
             Territory,Customer,Sum(SubTotal) As OrderValue
        From vwSalesOrderInfo 
    Group By Year(OrderDate),Sales_Person,Territory,Customer
    Order By OrderYear,Sales_Person,Territory,Customer
    

    This modified query answered all of our questions–or did it? Actually, it didn’t because the summaries given by this query offer summaries at the lowest “group” or “break” level. In this case it’s still the customer column. What we’re really looking for though is the ability to add additional “summary rows” in our result set that summarize the data by each of the grouping levels.

    To accomplish this, we could add subtotal to the above query by using a Common Table Expression (CTE) and including some UNION ALL statements to combine the total rows sets into detail data. We’d also probably need to do a few ORDER BY tricks to ensure the subtotal rows are placed in the proper sequence within the detail rows of the result set.

    This is where the super group concept comes into play. A super group will add summary rows to our query for each column or expression in the GROUP BY clause. For us old-time COBOL and RPG programmers, this is akin to “control break” totals. This new summary function is implemented by adding the WITH ROLLUP keywords to the GROUP BY clause:

      Select Year(OrderDate) As OrderYear,Sales_Person,
             Territory,Customer,Sum(SubTotal) As OrderValue
        From vwSalesOrderInfo 
    Group By Year(OrderDate),Sales_Person,Territory,Customer
    With Rollup  /* Implement Super Groups */
    Order By OrderYear,Sales_Person,Territory,Customer
    

    Here is a subset of the query’s results:

    Year

    Sales Person

    Territory

    Customer

    Order Value

    2001

    Ansman-Wolfe, Pamela

    Northwest

    A Bike Store

    $
    36,862.09

    2001

    Ansman-Wolfe, Pamela

    Northwest

    Alternative
    Vehicles

    $
    3,620.80

    2001

    Ansman-Wolfe, Pamela

    Northwest

    Capable Sales and
    Service

    $
    98,816.82

    2001

    Ansman-Wolfe, Pamela

    Northwest

    Moderately-Priced
    Bikes Store

    $
    17,081.92

    2001

    Ansman-Wolfe, Pamela

    Northwest

    Nonskid Tire
    Company

    $
    891.20

    2001

    Ansman-Wolfe, Pamela

    Northwest

    Outdoor Sports
    Supply

    $114,031.65

    2001

    Ansman-Wolfe, Pamela

    Northwest

    Raw Materials Inc

    $
    91,675.02

    2001

    Ansman-Wolfe, Pamela

    Northwest

    Scooters and Bikes
    Store

    $100,231.96

    2001

    Ansman-Wolfe, Pamela

    Northwest

    Some Discount Store

    $
    99,973.77

    2001

    Ansman-Wolfe, Pamela

    Northwest

    Thorough Parts and
    Repair Services

    $
    81,011.49

    2001

    Ansman-Wolfe, Pamela

    Northwest

    Vast Bike Sales and
    Rental

    $
    85,457.61

    2001

    Ansman-Wolfe, Pamela

    Northwest

    –

    $729,654.32

    2001

    Ansman-Wolfe, Pamela

    –

    –

    $729,654.32

     

    The rows with the purple cells are summary rows added to the query results compliments of ROLLUP. Each purple cell actually contains a NULL, which is how ROLLUP behaves for the column or group of columns it is summarizing. The first summarized row tells us that the total order value for sales rep “Pamela Ansman-Wolfe” within the Northwest territory region is about $730K. The next row indicates that Ansman-Wolfe sold the same amount for all territories. (In this case, this rep has only one territory.)

    If we look further down the result set we’ll see similar summary rows added after each change in territory, sales person, and year. Finally, ROLLUP courteously gives us a grand total row as well. Here are the last few rows of the result set:

    Year

    Sales Person

    Territory

    Customer

    Order Value

    2004

    Varkey Chudukatil,
    Ranjit

    France

    Wheels Inc.

    $ 440.17

    2004

    Varkey Chudukatil,
    Ranjit

    France

    –

    $ 1,658,518.07

    2004

    Varkey Chudukatil,
    Ranjit

    –

    –

    $ 1,658,518.07

    2004

    –

    –

    –

    $ 29,137,477.01

    –

    –

    –

    –

    $127,337,180.11

     

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Raz-Lee Security

    Start your Road to Zero Trust!

    Firewall Network security, controlling Exit Points, Open DB’s and SSH. Rule Wizards and graphical BI.

    Request Demo

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tolly Report Shows Reflection 2008 Outperforms Competitors, Attachmate Says The Data Center Is the Computer

    Leave a Reply Cancel reply

Volume 9, Number 9 -- March 11, 2009
None

Table of Contents

  • Looking for Commitment, Part 1
  • Treasury of new DB2 6.1 Features, Part 2: Grouping Sets and Super Groups
  • Admin Alert: Six Ways to Mess Up i5/OS User Profiles Security (And What To Do About It)

Content archive

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

Recent Posts

  • Public Preview For Watson Code Assistant for i Available Soon
  • COMMON Youth Movement Continues at POWERUp 2025
  • IBM Preserves Memory Investments Across Power10 And Power11
  • Eradani Uses AI For New EDI And API Service
  • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20

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