fhg
Volume 7, Number 33 -- September 26, 2007

Weird SQL UNION Results

Published: September 26, 2007

Hey, Ted:

I was intrigued by your article Have Your Cake and Eat It, Too. I decided to try your tip with some of my own data. The resulting data set had an anomaly in it. The system omitted a row of input, yet the total was still correct. Why?

--Wyatt

It's my bad, Wyatt, and I should have known better. (Note to self: Leave instructions for my survivors to put those last five words on my tombstone. It seems to accurately describe my life so far! ;)

The problem was that the example used the word UNION, but it should have used two words--UNION ALL. In my example, UNION gave me the right results. It didn't occur to me that UNION ALL was the proper solution.

First, allow me to give a little info about unions for the benefit of those who are not familiar with them. Unions combine two data sets into one. The intended purpose, according to set theory, is to combine two subsets of one superset. (See Thinking in Sets for an example of this usage.) But I seem to find unions more helpful in situations where two differing sets are being combined, as in the article you cited.

The reason the system omitted a row from your query, Wyatt, is that you had two identical rows in your input data. Yet the summary figures were correct because summary queries do not eliminate duplicate rows. Therefore you got something like this.

Name          Amt
-------   -------    
Jones       25.00
Smith       10.00
ZZ          45.00

So here's the rule of thumb that I forgot about when I wrote that tip. When you union two select queries, consider whether you are combining like data or unlike data. If you are combining like data (e.g., customers and customers) you probably want UNION. If you are combining unlike data (e.g., customers and vendors, or as in the referenced "Cake" article, customer details and summary totals), you probably want UNION ALL. My experience is that you will probably use UNION ALL more than UNION.

This rule of thumb does not always hold true, but you and the other readers of this publication are smart enough to determine when to go against the rule.

--Ted


RELATED STORIES

Have Your Cake and Eat It, Too

Thinking in Sets



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


Sponsored By
HELP/SYSTEMS

Discover Robot/SECURITY, the i5/OS security monitoring
and auditing software, from Help/Systems

                                                            · Profile Exchange
                                                            · Exit Point Monitoring
                                                            · System Authority Auditing
                                                            · QAUDJRN Monitoring
                                                            · System Security Analysis

Robot/SECURITY is the only security software that
combines five critical System i security tools in one package.

Visit our Web site at http://www.helpsystems.com/400g-sec/
to learn more about Robot/SECURITY.


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

COMMON:  Join us at the annual 2008 conference, March 30 - April 3, in Nashville, Tennessee
looksoftware:  Stop recoding with end-to-end modernization solutions
NowWhatJobs.net:  NowWhatJobs.net is the resource for job transitions after age 40


 

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
SAP Plants Its Flag in Mid-Market Territory with SaaS Apps

A1S Is to Applications What AS/400 Was to Systems

EGL: At Least It's Not Java, But It Ain't RPG, Either

As I See It: Shocking

The Linux Beacon
Canonical, VMware Create Skinny Linux for Virtual Appliances

HP Engineers New Blade Server Box for SMB Shops

SCO Files for Bankruptcy Protection

Transitive Rejiggers Emulation Software, Adds Partners

Four Hundred Stuff
Windows Vista Poses Challenges to Emulation Vendors

NetCustomer Capitalizes on Dissatisfaction with Oracle

Infor Provides Details on SOA Roadmap

Microsoft Ships BizTalk Server R2

Big Iron
Leverage

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
September 15, 2007: Volume 9, Number 37

September 8, 2007: Volume 9, Number 36

September 1, 2007: Volume 9, Number 35

August 25, 2007: Volume 9, Number 34

August 18, 2007: Volume 9, Number 33

August 11, 2007: Volume 9, Number 32

The Windows Observer
Microsoft Loses Antitrust Appeal in European Court

In Search Of a More Secure Internet

Sun and Microsoft Go All the Way with Windows

HP Engineers New Blade Server Box for SMB Shops

The Unix Guardian
SCO Files for Bankruptcy Protection

Sun and Microsoft Go All the Way with Windows

SAP Plants Its Flag in Mid-Market Territory with SaaS Apps

As I See It: The Dons of Dialogue

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

THIS ISSUE SPONSORED BY:

Help/Systems
WorksRight Software
COMMON


Printer Friendly Version


TABLE OF CONTENTS
Weird SQL UNION Results

A Snippet Above the Rest

Admin Alert: What Vendors Want to Know (and How to Get It)

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
What's coalesce good for?

Duplicated printer files

Urgent Help Needed--Limit the result set in SQL stored procedure

Problem with "cpyfrmimpf"

FNDSTRPDM Output Member Name to *OUTFILE





 
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