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

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags:

    Sponsored by
    ARCAD Software

    [IBM i Auditing Tool] Discover the Health of Your IBM i Applications in Minutes with ARCAD iCheck-up

    How well do you really know the health of your IBM i applications? Hidden complexity, unused code, and synchronization issues can make maintenance more challenging and slow down modernization efforts.

    What if you could identify these issues in just a few clicks, without installing anything on your IBM i system?

    Meet ARCAD iCheck-up, an auditing tool designed to give IBM i teams a clear, actionable overview of their applications in minutes.

    With iCheck-up, you can:

    • Assess code complexity: Identify complex source files and prioritize refactoring efforts.
    • Detect dead code: Uncover unused code that may be cluttering your applications.
    • Spot synchronization issues: Identify inconsistencies between source files and compiled objects.
    • Track code changes: Explore modification history and better understand your application’s evolution.
    • Generate a shareable report: Get a comprehensive, easy-to-read HTML report with key metrics and insights.

    Fast, simple, and secure. iCheck-up runs from Windows, requires no installation on your IBM i system, and leaves no files or code behind. Your generated report contains metrics only, keeping your source code confidential.

    Whether you’re maintaining existing applications, planning modernization, or looking to improve code quality, iCheck-up helps you make more informed decisions.

    Ready to gain deeper insights into your IBM i applications? Discover ARCAD iCheck-up and take the first step toward better code quality today!

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) 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!

    A Second Look At SQL Descriptors New OLAP Aggregate Functions In DB2 For i, Part 2

    Leave a ReplyCancel 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

  • Fall TRs Expected Next Week At Combined IBM-COMMON Conference
  • GenAI Brings A New Kind Of Agile Computing That Needs Controls Even More
  • IBM i, AI, And The Cost Of “We’ve Always Done It That Way”
  • Infor Says IBM i Customers Are Welcome To Use New AI
  • IBM i PTF Guide, Volume 28, Number 36
  • Help Us Understand The IBM i Marketplace By Taking The Survey
  • IBM Goes GA With Power Autonomous Operations
  • IBM Wheels And Deals To Sell Power Systems, Storage, and Software
  • Guru: When RNF0722 Won’t Let Your Legendary RPG Program Compile
  • A Read-Only MCP Server For Db2 for i

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