• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Weird SQL UNION Results

    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

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Midrange Dynamics North America

    Git up to speed with MDChange!

    Git can be lightning-fast when dealing with just a few hundred items in a repository. But when dealing with tens of thousands of items, transaction wait times can take minutes.

    MDChange offers an elegant solution that enables you to work efficiently any size Git repository while making your Git experience seamless and highly responsive.

    Learn more.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    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

    B&L Releases Cost Estimating Wizard State of the System i: What’s Going On in the Market?

    Leave a Reply Cancel reply

Volume 7, Number 33 -- September 26, 2007
THIS ISSUE SPONSORED BY:

Help/Systems
WorksRight Software
COMMON

Table of Contents

  • Weird SQL UNION Results
  • A Snippet Above the Rest
  • Admin Alert: What Vendors Want to Know (and How to Get It)

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Public Preview For Watson Code Assistant for i Available Soon
  • COMMON Youth Movement Continues at POWERUp 2025
  • IBM Preserves Memory Investments Across Power10 And Power11
  • Eradani Uses AI For New EDI And API Service
  • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20

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