fhg
Volume 7, Number 16 -- April 25, 2007

What Can I Select When I Group?

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


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
E-mail: software@worksright.com
Web site: www.worksright.com


Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

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

 

The Four Hundred
Power6: Later in 2007 Rather than Sooner?

Slowing U.S. Sales Hurt IBM's First Quarter

Reader Feedback on User-Priced System i Boxes

As I See It: Induced Labor

The Linux Beacon
FastScale Takes a Different Approach to Virtualization and Provisioning

Sun, Canonical Integrate Java, GlassFish, and NetBeans into Ubuntu

Round Two: Intel's Fortunes Rise, and AMD's Fall

Slowing U.S. Sales Hurt IBM's First Quarter

Four Hundred Stuff
PowerTech Tools Build Trust By Decreasing Authority

IBM Expects Speedier Portal Projects

BSafe Introduces Cross-Platform Auditing

CCSS Addresses SOX Requirements in QMessage Monitor

Big Iron
Slowing U.S. Sales Hurt IBM's First Quarter

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
April 21, 2007: Volume 9, Number 16

April 14, 2007: Volume 9, Number 15

April 7, 2007: Volume 9, Number 14

March 31, 2007: Volume 9, Number 13

March 24, 2007: Volume 9, Number 12

March 17, 2007: Volume 9, Number 11

The Windows Observer
'Viridian' Beta Delayed. Is Longhorn Next?

Windows Server DNS Flaw Being Exploited

Dell, IBM Push Power-Saving Servers

Marathon Makes Virtualization Fault Tolerant with v-Available

The Unix Guardian
Fujitsu, Sun Deliver Joint Sparc Enterprise Server Line

Power6: Later in 2007 Rather than Sooner?

Slowing U.S. Sales Hurt IBM's First Quarter

As I See It: Disorderly Conduct

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

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, Part 1

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
User profile for webserver instances

Specific Record Range (CL)

Select/omit error on member

Encoded Vector Index(EVI)--Throw some light

Kurt Vonnegut is dead





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement