• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Conditional SQL I/O

    June 5, 2013 Ted Holt

    If an RPG programmer has a best friend, it may very well be the CHAIN opcode, which performs a random read on a keyed database file. It’s common programming practice in information-processing shops to read one file sequentially and to chain to read a host of others randomly. SQL handles this sort of thing differently. With SQL, joining is the proper way to retrieve data from two or more tables and/or views. The difference is even more pronounced when some of the random processing may or may not take place.

    Suppose you’re responsible for a database that contains customer orders. Each order is for one item, and the price of an item comes from one of two tables: a standard pricing table (the catalog prices), and a customer-specific pricing table. The usual way to retrieve the price is to look for (randomly read) a customer-specific price first, and if none is found, then and only then look for (randomly read) the standard price. In RPG terms, it’s like this:

    dow '1';
       read order;
       if %eof();
          leave;
       endif;
       // look for a price for this customer & item
       chain (custid: itemid) cusprice;
       if %found();
          SellingPrice = Price;
       else;
          // there was no price for this customer
          // get the catalog price
          chain itemid PriceRec;
          if %found();
             SellingPrice = Price;
          else;
             // there is no price for item
             // perform error routine
          endif;
       endif;
    enddo;
    

    But SQL contains no such conditional logic to control I/O. In an SQL world, the way to handle such a situation is to read everything, whether you need it or not. This is counterintuitive to an old RPG or COBOL programmer, but it works, and it works just fine.

    Take a look at this SQL command:

    select ord.ordernbr, ord.custid, ord.itemid,
           cp.price, prc.price,
           coalesce(cp.price, prc.price) as SellingPrice
      from orders as ord
      left join prices as prc
        on ord.itemid = prc.itemid
      left join cusprices as cp
        on ord.custid = cp.custid
       and ord.itemid = cp.itemid
    

    The primary table, ORDERS, is the one that would typically be read sequentially in an RPG or COBOL program. PRICES (the standard price table) and CUSPRICES (the customer price table) are both included as secondary tables. The conditional logic–giving preference to the customer price over the standard price–is carried out with the COALESCE function.

    COALESCE takes two or more arguments and returns the first one that is not null. In this case, if the customer price (cp.price) is not null, the customer price is the selling price. Otherwise, the standard price (prc.price) is the selling price. The result set illustrates this behavior.

    Order number

    Customer ID

    Item ID

    Customer price

    Standard price

    Selling price

    101

    3

    A1

    null

    1.50

    1.50

    102

    1

    A1

    1.25

    1.50

    1.25

    103

    5

    A1

    null

    1.50

    1.50

    104

    6

    A1

    null

    1.50

    1.50

    105

    8

    A1

    1.10

    1.50

    1.10

    Notice that customer price overrides the standard price for orders 102 and 105.

    Let’s complicate the example a bit.

    Suppose that a customer can belong to another customer (sometimes called a parent or a group). If a customer has negotiated a specific price for an item, that price is to be used in invoicing. If there is no customer-specific price, then the parent’s (group) price is to be used, if such exists. Otherwise, we’re back to the standard price.

    The concept is the same. All necessary tables are joined to form the result set, and COALESCE picks the first non-null of the three values.

    select ord.ordernbr, ord.custid, cus.custgrp,
           ord.itemid, cp.price, gp.price, prc.price,
           coalesce(cp.price, gp.price, prc.price) as UnitPrice
      from orders as ord
      left join prices as prc
        on ord.itemid = prc.itemid
      left join cusprices as cp
        on ord.custid = cp.custid
       and ord.itemid = cp.itemid
      left join customers as cus
        on ord.custid = cus.custid
      left join cusprices as gp
        on cus.cusgrp = gp.custid
    

    Table CUSPRICES is used twice in the query; once for a customer (CP) and once for the customer’s group (GP). Here’s the result set:

    Order number

    Customer ID

    Customer group

    Item ID

    Customer price

    Group price

    Standard price

    Selling price

    101

    3

    0

    A1

    null

    null

    1.50

    1.50

    102

    1

    0

    A1

    1.25

    null

    1.50

    1.25

    103

    5

    2

    A1

    null

    1.35

    1.50

    1.35

    104

    6

    7

    A1

    null

    null

    1.50

    1.50

    105

    8

    2

    A1

    1.10

    1.35

    1.50

    1.10

    Customer price comes from the CUSPRICES row (record) for the customer. Group price comes from the CUSPRICES row for the parent. COALESCE picks the first non-null of the three values, forcing customer price to override the parent price and parent price to override the standard price.

    I’ll mention one more thing, and then I’m done. If this query were from a real production system, I would not include the customer price, group price, and standard price in the result set unless there was a need to do so. The only price I would typically include would be the selling price. I would probably not include the customer group either.

    Order number

    Customer ID

    Item ID

    Selling price

    101

    3

    A1

    1.50

    102

    1

    A1

    1.25

    103

    5

    A1

    1.50

    104

    6

    A1

    1.50

    105

    8

    A1

    1.10

    The fact that I use a column (field) in a query doesn’t mean that I have to put it in the result set.



                         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
    DRV Tech

    Get More Out of Your IBM i

    With soaring costs, operational data is more critical than ever. IBM shops need faster, easier ways to distribute IBM applications-based data to users more efficiently, no matter where they are.

    The Problem:

    For Users, IBM Data Can Be Difficult to Get To

    IBM Applications generate reports as spooled files, originally designed to be printed. Often those reports are packed together with so much data it makes them difficult to read. Add to that hardcopy is a pain to distribute. User-friendly formats like Excel and PDF are better, offering sorting, searching, and easy portability but getting IBM reports into these formats can be tricky without the right tools.

    The Solution:

    IBM i Reports can easily be converted to easy to read and share formats like Excel and PDF and Delivered by Email

    Converting IBM i, iSeries, and AS400 reports into Excel and PDF is now a lot easier with SpoolFlex software by DRV Tech.  If you or your users are still doing this manually, think how much time is wasted dragging and reformatting to make a report readable. How much time would be saved if they were automatically formatted correctly and delivered to one or multiple recipients.

    SpoolFlex converts spooled files to Excel and PDF, automatically emailing them, and saving copies to network shared folders. SpoolFlex converts complex reports to Excel, removing unwanted headers, splitting large reports out for individual recipients, and delivering to users whether they are at the office or working from home.

    Watch our 2-minute video and see DRV’s powerful SpoolFlex software can solve your file conversion challenges.

    Watch Video

    DRV Tech

    www.drvtech.com

    866.378.3366

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Profound Logic Software:  Live Webinar: How to Take IBM i Everywhere with Mobile Apps. June 26
    BCD:  Reduce the IT reporting blues with Monarch for EZ-Pickin's
    RJS Software Systems:  Replace unsupported legacy forms or upgrade to iForms.

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ The Reg: More News From ITJ EIC Timothy Prickett Morgan

    TMW Delivers Web Interface for IBM i Trucking App IBM Rolls Out PureFlex-IBM i Bundle With Decent Discounts

    Leave a Reply Cancel reply

Volume 13, Number 11 -- June 5, 2013
THIS ISSUE SPONSORED BY:

ProData Computer Services
WorksRight Software
System i Developer

Table of Contents

  • When Is A Source Member Not A Source Member?
  • Conditional SQL I/O
  • Admin Alert: Renewing A Default *System Certificate

Content archive

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

Recent Posts

  • 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
  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19

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