• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Throw Away Old Summary Query Techniques

    January 12, 2016 Ted Holt

    It’s a new year! Out with the old! In with the new! That exhortation applies not only to everyday life, but also to the ways we write SQL queries. Let me show you two ways we used to have to write summary queries and two new ways that are better.

    First, we need a database to query. Here’s a table of customers.

     ID   Name
    ===   =====================
    101   J. Cheever Loophole  
    102   Otis B. Driftwood    
    103   Quincy Adams Wagstaff
    104   Rufus T. Firefly
    

    And here’s a table of shipments.

    ID   CUSTOMER   ITEM    QUANTITY   PRICE
    ===  ========   ======  ========   =====
     1       102    AB-200       2       .25
     2       104    BC-300       4      1.00
     3       102    AB-200       1       .20
     4       101    EF-303       2      1.00
     5       102    BB-772       6       .10
    

    Now, suppose you’ve been asked to sum up these sales by customer. For a professional like yourself, that’s easy.

    select customer, sum(quantity * price)
      from sales
     group by customer
     order by customer
    
    CUSTOMER     SUM
    ========    ====
         101    2.00
         102    1.30
         104    4.00
    

    Oh, but now the requestor asks you to include the name of each customer. Since the customer name is not in the sales file, you must add the customer table to the query.

    You could do this:

    select s.customer, c.name,
           sum(s.quantity * s.price)
      from sales as s             
      join cust as c              
        on s.customer = c.id        
     group by s.customer, c.name
     order by s.customer            
    
    CUSTOMER   NAME                      SUM
    ========   =====================    ====
         101   J. Cheever Loophole      2.00
         102   Otis B. Driftwood        1.30
         104   Rufus T. Firefly         4.00
    

    It gets the proper results, but to do so it must include the customer name as a summary column. (That is, customer name had to be added to the GROUP BY clause.) Adding this unnecessary grouping column (field) is extra work that can increase the run time when the query runs over a large amount of data.

    You could do this instead:

    select s.customer, max(c.name),
           sum(s.quantity * s.price)
      from sales as s             
      join cust as c              
        on s.customer = c.id        
     group by s.customer            
     order by s.customer            
    
    CUSTOMER   MAX(NAME)                 SUM
    ========   =====================    ====
         101   J. Cheever Loophole      2.00
         102   Otis B. Driftwood        1.30
         104   Rufus T. Firefly         4.00
    

    The customer name is no longer a summary column, but notice the MAX function in SELECT. Since the customer name is functionally dependent on customer ID, the system will waste time finding the maximum of many identical values. It’s extra work, and it’s also misleading, as it implies that the value of NAME may be different for sales to the same customer number.

    I used to use such methods years ago, when SQL was less powerful, but I don’t use them now and I don’t recommend them to you. Instead, I would access the summary query as if it were a table or view:

    select s.customer, c.name, s.sales
      from (select customer,
                   sum(quantity * price) as sales
              from sales
             group by customer) as s
      join cust as c
        on s.customer = c.id
     order by s.customer
    
    CUSTOMER   NAME                      SUM
    ========   =====================    ====
         101   J. Cheever Loophole      2.00
         102   Otis B. Driftwood        1.30
         104   Rufus T. Firefly         4.00
    

    The customer name is not a summary field and there is no MAX function. The system runs the inner query first to build the summary figures, then joins that result set to the customer master table to get the customer name.

    You could also use a common table expression.

    with s as
      (select customer,
              sum(quantity * price) as sales
         from sales
        group by customer)
                  
     select s.customer, c.name, s.sales
      from s
      join cust as c
        on s.customer = c.id
     order by s.customer
    

    I still see queries like the first two. Maybe old habits die hard.

    Not only do I encourage people to use the newer techniques, I encourage them to consider rewriting any old queries that use these techniques.

    Replacing old techniques with new ones may seem like a small thing, but small things can make a big difference. As Jim Rohn said, “Success is doing ordinary things extraordinarily well.”

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Maxava

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Townsend Security:  IBM Security QRadar, IBM i and Townsend Security - Better Together
    System i Developer:  RPG & DB2 Summit - March 22-24 in Dallas. Register by Feb 12 and save $300!
    United Computer Group:  VAULT400 BaaS delivers secure cloud backup and DR solutions

    ARCAD Sets Out To Modernize German Midrange The Data-Centric Depiction Of IBM i

    Leave a Reply Cancel reply

Volume 16, Number 01 -- January 12, 2016
THIS ISSUE SPONSORED BY:

Bug Busters Software Engineering
WorksRight Software
System i Developer

Table of Contents

  • Updated RDi Keyboard Shortcuts
  • Throw Away Old Summary Query Techniques
  • SQL Paging With Limit And Offset In DB2 For i

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