• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • What Can I Select When I Group?

    April 25, 2007 Dear highly esteemed professional colleague

    It’s not uncommon for someone to ask me for help with error SQL0122, which involves SQL commands that use the GROUP BY clause to produce summary figures. Since I see this error over and over and over, I thought it would be good to explain how summary queries work. The standard rule I hear is that the SELECT clause of summary queries can list aggregate fields (those in the GROUP BY clause), expressions that are in the GROUP BY clause, and column functions, such as COUNT, SUM, and AVG. That’s close, but slightly inaccurate.

    A reader who contacted me recently was working with a field that had, among other things, a character date in MMDDYYYY format and a currency value. He needed to summarize the file by date. His first attempt looked something like this:

    SELECT SomeDate, SUM(SomeNumber)
      FROM mydata
     GROUP by SomeDate
     ORDER BY SomeDate
    

    The SELECT clause contains a grouping field (SomeDate) and a column function, SUM. If any non-grouping fields had been in the SELECT clause, SQL0122 (Column WAREHOUSE or expression in SELECT list not valid.) would have been the result.

    SELECT SomeDate, Warehouse, SUM(SomeNumber)
      FROM mydata
     GROUP by SomeDate
     ORDER BY SomeDate
    

    Let’s go back to the reader’s query. The query returned accurate data, but the result set was sequenced by month, then day, then year.

    SomeDate     Sum
    ========     ===
    01022007      25 
    07032005      50 
    09032002      95 
    11201998      35 
    12312006     125
    

    This is not what he wanted. He needed to format the data so that it would sort in proper chronological sequence. I had him replace SomeDate with an expression that would convert the date to YYYYMMDD format.

    SELECT substr(SomeDate,5,4)||substr(SomeDate,1,4), 
           SUM(SomeNumber)
      FROM mydata
     GROUP by substr(SomeDate,5,4)||substr(SomeDate,1,4)
     ORDER BY substr(SomeDate,5,4)||substr(SomeDate,1,4)
    

    And this is the result set.

    SomeDate     Sum
    ========     ===
    19981120      35 
    20020903      95 
    20050703      50 
    20061231     125 
    20070102      25
    

    It’s OK to group and sort by expressions as long as they match the expressions in the SELECT clause. But that does not mean that the expressions in the SELECT clause have to exactly match the arguments in the GROUP BY clause. For example, here is another version of the previous query that returns the same results.

    SELECT substr(SomeDate,5,4)||substr(SomeDate,1,4),
           SUM(SomeNumber)
      FROM mydata
     GROUP by SomeDate
     ORDER BY 1
    

    The SELECT clause and GROUP BY clause do not match. They don’t have to match, because the date-conversion expression in the SELECT clause does not reference a non-grouping field. The system can group the data by the distinct date values, but present those values in a different format.

    Here’s another allowable expression that does not match the GROUP BY clause. While the date is still used as a grouping field, the record set returns that date two weeks into the future.

    SELECT DATE(substr(SomeDate,5,4)||'-'||
                substr(SomeDate,1,2)||'-'||
                substr(SomeDate,3,2))
                + 14 days,
           SUM(SomeNumber)
      FROM mydata
     GROUP by SomeDate
     ORDER BY 1
    

    An expression can even combine two or more grouping fields. To illustrate, assume a database file, three of whose fields are a terms code, a credit code, and an order value. If the concatenation of the credit code and terms code is meaningful in some way, you might use a query of this kind:

    SELECT CreditCode||TermCode, SUM(OrderValue)
       FROM mydata
      GROUP by CreditCode, TermCode
      ORDER BY 1
    

    The result set has two columns. The first is the credit code concatenated to the terms code. The second is the sum of the order values.

    You might also do something of this nature:

    SELECT CreditCode, TermCode, CreditCode||TermCode, 
           SUM(OrderValue)
      FROM mydata
     GROUP by CreditCode, TermCode
     ORDER BY 1
    

    Here the grouping fields are in the result set, along with their concatenation.

    You may even build something like this, where the TermsCode is rearranged:

    SELECT CreditCode||substr(TermCode,2,1)||substr(TermCode,1,1),
           SUM(OrderValue)
       FROM mydata
      GROUP by CreditCode, TermCode
      ORDER BY 1
    

    The two-byte terms code is reversed and appended to the credit code to create the first column of the result set.

    The point is that your result set layout, as defined in the SELECT clause, is not required to match the GROUP BY clause. You are free to build any expressions, provided you do not refer to non-grouping fields.

    –Ted



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    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

    New Generation Software:  Leading provider of iSeries BI and financial management software
    Vision Solutions:  The first new HA release from the newly merged Vision and iTera companies
    LASERTEC USA:  Fully integrate MICR check printing with your existing application

    IT Jungle Store Top Book Picks

    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database Guide: List Price, $59.00
    The iSeries Pocket Developers' Guide: List Price, $59.00
    The iSeries Pocket SQL Guide: List Price, $59.00
    The iSeries Pocket Query Guide: List Price, $49.00
    The iSeries Pocket WebFacing Primer: List Price, $39.00
    Migrating to WebSphere Express for iSeries: List Price, $49.00
    iSeries Express Web Implementer's Guide: List Price, $59.00
    Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    WebFacing Application Design and Development Guide: List Price, $55.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    The All-Everything Machine: List Price, $29.95
    Chip Wars: List Price, $29.95

    Help/Systems Issues Another Update for Robot/SCHEDULE The i5 515 and 525: IBM’s Competitive Analysis

    Leave a Reply Cancel reply

Volume 7, Number 16 -- April 25, 2007
THIS ISSUE SPONSORED BY:

WorksRight Software
Help/Systems
Guild Companies

Table of Contents

  • What Can I Select When I Group?
  • To Shift or Not to Shift: That Is in the Fourth Parameter
  • Admin Alert: Dealing with i5 Critical Storage Errors,
  • The Long and Short of Setting Up Level 40 Security

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