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

    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

  • To Comfort The Afflicted And Afflict The Comfortable
  • How FalconStor Is Reinventing Itself, And Why IBM Noticed
  • Guru: When Procedure Driven RPG Really Works
  • Vendors Fill In The Gaps With IBM’s New MFA Solution
  • IBM i PTF Guide, Volume 27, Number 27
  • With Power11, Power Systems “Go To Eleven”
  • With Subscription Price, IBM i P20 And P30 Tiers Get Bigger Bundles
  • Izzi Buys CNX, Eyes Valence Port To System Z
  • IBM i Shops “Attacking” Security Concerns, Study Shows
  • IBM i PTF Guide, Volume 27, Number 26

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