fhg
Volume 6, Number 21 -- May 24, 2006

Thinking in Sets

Published: May 24, 2006

Hey, Ted:

I have a program that contains a complicated SQL command that selects a group of records exactly the way I want them selected. My boss has added a new wrinkle by creating an exceptions file, which would indicate other records that are to be selected, whether they meet the selection criteria or not. I cannot find a way to add the exceptions file without breaking my query. How do I modify my SQL command to handle the exceptions file?

--Clayton


You were taught the solution to your problem in elementary school. Do you remember learning about sets in math class? E. F. "Ted" Codd based the whole idea of relational databases on set theory.

A set is an unordered group of like things. The students in the classroom were a set. The boys in the class were a set. The redheaded boys were a set.

Your teacher and textbook explained to you that you can relate sets in different ways. For example, overlapping the set of redheaded boys with the set of boys who play baseball gives you the set of redheaded baseball-playing boys. This is known as the intersection of two sets.

You might combine the two sets into one, in which case the set contains all boys who are redheaded and all boys who like to play baseball. Some of the boys might fit in both categories, but all such boys appear in the result set only once, as there cannot be two instances of one person. This is known as the union of two sets.

You can also remove the members of one set from another set. For instance, you might remove the redheaded boys from the set of baseball players, which results in the set of baseball players who are not redheaded. Or you could remove the baseball players from the set of redheaded boys, which gives you the set of redheaded boys who do not play baseball. This operation is known as set minus.

So which of these set operations solves your problem? Union. You need the union of the set of things that pass the rigorous testing of your WHERE clause and the set of things that are to be included whether they satisfy the WHERE clause or not.

Let me illustrate these set operations with a simpler example. Consider a set of customers, stored in file QIWS/QCUSTCDT.


R#CUSNUMLSTNAMINITCITY STATECDTLMTBALDUE
1938472Henning G KDallas TX500037
2839283Jones B DClay NY400100
3392859Vine S SBroton VT700439
4938485Johnson J AHelen GA99993987.5
5397267Tyron W EHector NY10000
6389572Stevens K LDenver CO40058.75
7846283Alison J SIsle MN 500010
8475938Doe J WSutter CA700250
9693829Thomas A NCasper WY99990
10593029WilliamsE DDallas TX20025
11192837Lee F LHector NY700489.5
12583990Abraham M TIsle MN9999500

















Let's suppose that your query finds customers who owe your company more than 250 simoleons (or whatever your local currency is.)

SELECT cusnum, lstnam, init, city, state,
       cdtlmt, baldue
  FROM qcustcdt
 WHERE baldue > 250

R# CUSNUMLSTNAMINITCITY STATECDTLMTBALDUE
1392859Vine S SBroton VT700439
2938485Johnson J AHelen GA99993987.5
3192837Lee F LHector NY700489.5
4583990Abraham M TIsle MN9999500








Now let's see who's in the exceptions file.

SELECT CusNumber
  FROM custexcept
 ORDER BY 1
R#CUSNUMBER
1389572
2392859
3593029







The only thing lacking is to combine the two sets into one.

SELECT cusnum, lstnam, init, city, state,
       cdtlmt, baldue
  FROM qcustcdt
 WHERE baldue > 250
UNION
SELECT c.cusnum, c.lstnam, c.init,
       c.city, c.state, c.cdtlmt, c.baldue
  FROM custexcept AS e
  JOIN qcustcdt AS c
    ON e.CusNumber = c.cusnum
    ORDER BY 1

R# CUSNUMLSTNAMINITCITY STATECDTLMTBALDUE
1192837Lee F LHector NY700489.5
2389572Stevens K LDenver CO40058.75
3392859Vine S SBroton VT700439
4583990Abraham M TIsle MN9999500
5593029WilliamsE DDallas TX20025
6938485Johnson J AHelen GA99993987.5











Notice that customer S. S. Vine is included once, not twice. The normal behavior of UNION is to omit duplicate rows. If you do not want to drop duplicate rows, use UNION ALL instead of UNION.

That takes care of your problem, Clayton. For sake of completeness, I'll illustrate the other two set operations. These only work at V5R3 and above.

First, here's the intersect operation, which returns only the rows that are members of both sets.

SELECT cusnum, lstnam, init, city, state,   
       cdtlmt, baldue                       
  FROM qcustcdt                        
 WHERE baldue > 250                         
INTERSECT                                   
SELECT c.cusnum, c.lstnam, c.init,          
       c.city, c.state, c.cdtlmt, c.baldue  
  FROM custexcept AS e               
  JOIN qcustcdt AS c                   
    ON e.CusNumber = c.cusnum               
    ORDER BY 1                              

R# CUSNUMLSTNAMINITCITY STATECDTLMTBALDUE
3392859Vine S SBroton VT700439





Here's the set minus. You would use this operation if the purpose of the exceptions file were to exclude certain customers.

SELECT cusnum, lstnam, init, city, state, 
       cdtlmt, baldue                     
  FROM qcustcdt                      
 WHERE baldue > 250                       
EXCEPT                                    
SELECT c.cusnum, c.lstnam, c.init,        
       c.city, c.state, c.cdtlmt, c.baldue
  FROM custexcept AS e             
  JOIN qcustcdt AS c                 
    ON e.CusNumber = c.cusnum             
    ORDER BY 1                            

R# CUSNUMLSTNAMINITCITY STATECDTLMTBALDUE
1192837Lee F LHector NY700489.5
4583990Abraham M TIsle MN9999500
6938485Johnson J AHelen GA99993987.5







--Ted



Sponsored By
PRODATA COMPUTER SERVICES

ServerProven Tool Virtually FREE!

DBU...database access made simple
DBU Audit...log changes & views made via DBU
DBUnifier...unify database apps & dump DFUs!
RSP...RPG Server Pages for Web enablement
STE...Stored Procedure Test Environment
SQL/Pro...query & reporting tools

All NEW ServerProven products that are virtually FREE with IBM's rebate offering!

Sign up to WIN a Video iPod!
800.228.6318
www.DoDBU.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

Bug Busters Software Engineering:  Quality software solutions for the iSeries since 1988
COMMON:  Join us at the Fall 2006 conference, September 17-21, in Miami Beach, Florida
Xperia:  Fully integrated suite of applications for small- to mid-sized companies

 


 
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