• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Side-By-Side Lists

    July 19, 2016 Hey, Ted

    An end user has asked me to provide him a spreadsheet with two independent lists, one beside the other. Can I use SQL to satisfy his request?

    –Dennis

    Yes, you can. Dennis works for a manufacturer, and his data had to do with routing operations. I’m going to use a more common type of data to illustrate.

    Let’s say we work for a company that sells dohickeys, thingamajigs, and whatchamacallits. We offer these items in various colors, but we don’t offer all items in all colors.

    ITEM

    COLOR

    Dohickey

    Blue

    Dohickey

    Purple

    Thingamajig

    Green

    Thingamajig

    Red

    Thingamajig

    Yellow

    Whatchamacallit

    Blue

    Whatchamacallit

    Green

    Whatchamacallit

    Orange

    Whatchamacallit

    Purple

    Whatchamacallit

    Red

    Whatchamacallit

    Yellow

    We offer the items in various sizes, but we don’t offer all items in all sizes.

    ITEM

    SIZE

    Dohickey

    Tiny

    Dohickey

    Small

    Dohickey

    Medium

    Dohickey

    Large

    Dohickey

    Enormous

    Thingamajig

    Small

    Thingamajig

    Medium

    Thingamajig

    Large

    Whatchamacallit

    Small

    Whatchamacallit

    Medium

    Whatchamacallit

    Large

    Color and size are not related to one another. That is, we don’t offer, for example, medium whatchamacallits in red and purple, but large whatchamacallits in red, purple, and green. If we did, the data would be stored differently in the tables.

    To build side-by-side lists is easy if you use the ROW_NUMBER OLAP function. Here’s the query:

    with f1 as
       (select Item, Color,
               row_number() over (partition by Item) as RowNbr
          from ItemColors),
     f2 as
       (select Item, Size,
               row_number() over (partition by Item) as RowNbr
          from ItemSizes)
    select coalesce(f1.Item, f2.Item), f1.color, f2.size
      from f1 full outer join f2
        on (f1.item, f1.rownbr) = (f2.item, f2.RowNbr)
     order by 1
    

    And here’s the result set:

    ITEM

    COLOR

    SIZE

    Dohickey

    Blue

    Tiny

    Dohickey

    Purple

    Small

    Dohickey

     

    Medium

    Dohickey

     

    Large

    Dohickey

     

    Enormous

    Thingamajig

    Red

    Small

    Thingamajig

    Green

    Medium

    Thingamajig

    Yellow

    Large

    Whatchamacallit

    Red

    Small

    Whatchamacallit

    Blue

    Medium

    Whatchamacallit

    Green

    Large

    Whatchamacallit

    Yellow

     

    Whatchamacallit

    Orange

     

    Whatchamacallit

    Purple

     

    The blank colors and sizes are null values.

    So how does this work? First, the common table expressions use the ROW_NUMBER function to number the rows. Here are the colors:

    select Item, Color,
           row_number() over (partition by Item) as RowNbr
      from ItemColors
    

    ITEM

    COLOR

    ROWNBR

    Dohickey

    Blue

    1

    Dohickey

    Purple

    2

    Thingamajig

    Red

    1

    Thingamajig

    Green

    2

    Thingamajig

    Yellow

    3

    Whatchamacallit

    Red

    1

    Whatchamacallit

    Blue

    2

    Whatchamacallit

    Green

    3

    Whatchamacallit

    Yellow

    4

    Whatchamacallit

    Orange

    5

    Whatchamacallit

    Purple

    6

    ROW_NUMBER numbers the rows. Think of PARTITION BY as a control break. Thanks to PARTITION BY, numbering restarts with each item.

    It works the same way for sizes.

    select Item, Size,
           row_number() over (partition by Item) as RowNbr
      from ItemSizes
    

    ITEM

    SIZE

    ROWNBR

    Dohickey

    Tiny

    1

    Dohickey

    Small

    2

    Dohickey

    Medium

    3

    Dohickey

    Large

    4

    Dohickey

    Enormous

    5

    Thingamajig

    Small

    1

    Thingamajig

    Medium

    2

    Thingamajig

    Large

    3

    Whatchamacallit

    Small

    1

    Whatchamacallit

    Medium

    2

    Whatchamacallit

    Large

    3

    To build the side-by-side lists is a simple matter of joining the two common table expressions on item and row number. Since an item may be offered in more or fewer colors than sizes, this query requires a full outer join.

    select coalesce(f1.Item, f2.Item), f1.color, f2.size
      from f1 full outer join f2
        on (f1.item, f1.rownbr) = (f2.item, f2.RowNbr)
     order by 1
    

    ITEM

    COLOR

    SIZE

    Dohickey

    Blue

    Tiny

    Dohickey

    Purple

    Small

    Dohickey

     

    Medium

    Dohickey

     

    Large

    Dohickey

     

    Enormous

    Thingamajig

    Red

    Small

    Thingamajig

    Green

    Medium

    Thingamajig

    Yellow

    Large

    Whatchamacallit

    Red

    Small

    Whatchamacallit

    Blue

    Medium

    Whatchamacallit

    Green

    Large

    Whatchamacallit

    Yellow

     

    Whatchamacallit

    Orange

     

    Whatchamacallit

    Purple

     

    This is not a common query, and I would not call the result set relational. However, our job is to help users do their jobs, not to be purists.

    Ted Holt welcomes your comments and questions. Email him through the IT Jungle Contacts page.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    New Generation Software

    FREE Webinar:

    Creating Great Data for Enterprise AI

    Enterprise AI relies on many data sources and types, but every AI project needs a data quality, governance, and security plan.

    Wherever and however you want to analyze your data, adopting modern ETL and BI software like NGS-IQ is a great way to support your effort.

    Webinar: June 26, 2025

    RSVP today.

    www.ngsi.com – 800-824-1220

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    System i Developer:  RPG & DB2 Summit - October 4-6 2016 in Chicago. Register now!
    COMMON:  Open Source and Systems Management at the COMMON Forum. August 24-25 in Chicago.
    LaserVault:  Webinar → Simplify IBM i backup and recovery. July 27, 2pm EST. Enter to win a drone!

    New OLAP Aggregate Functions In DB2 for i, Part 1 Blue Stack Deadline Looms for JD Edwards Shops

    Leave a Reply Cancel reply

Volume 16, Number 16 -- July 19, 2016
THIS ISSUE SPONSORED BY:

WorksRight Software
T.L. Ashford
LaserVault

Table of Contents

  • A Second Look At SQL Descriptors
  • Side-By-Side Lists
  • New OLAP Aggregate Functions In DB2 For i, Part 2

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