• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Creating Yes/No Fields in SQL Queries

    January 27, 2010 Hey, Ted

    I want to retrieve a list of customers using SQL. I know how to select the customers I want. What I don’t know how to do is create some columns that are not stored in the database, but must be built from information in other files. For example, I want a column that tells whether or not the customer has an open (unfulfilled) order. I’d also like a column that tells if he has ever bought a certain line of product. There are no fields in the customer master file that store this information. Can you help?

    –Rick

    Good question, Rick. You’ll be glad to hear that this is not hard to do.

    Let’s assume you want all type-A customers (whatever that means).

    select c.customerno, c.cusnam
      from customers as c
     where c.type = 'A'
    

    Here they are:

    CUSTOMERNO  CUSNAM
      12345     Cal E. Phornya
      23456     Billy Rubin
      34567     Polly Unsaturated
      45678     Molly Coddle
    

    Let’s assume that closed (or fulfilled) orders are indicated by a status code of “9” in the sales order header file. Here are the customers with open orders.

    select distinct o.customerno, 'Yes' as HasActiveOrder
      from salesordh as o
     where o.status <> '9'
    
    CUSTOMERNO  HASACTIVEORDER
      34567         Yes
      45678         Yes
      12345         Yes
      56789         Yes
    

    Notice the use of DISTINCT to eliminate duplicate customer numbers from the list. Notice also that the literal “Yes” fills up a column named HasActiveOrder.

    As for the customers who have ever ordered a certain line of product, let’s assume the product line is in a class field in an item master file, and that the items a customer has ordered are in the sales order details file. Here are the customers who’ve ordered items from product line B3.

    select distinct o.customerno, 'Yes' as BoughtB3
      from salesordh as o
      join salesordd as d
        on o.orderno = d.orderno
      join items as i
        on d.itemno = i.itemno
     where i.class='B3'
    
    CUSTOMERNO  BOUGHTB3
      34567      Yes
      23456      Yes
    

    This is the same sort of thing. DISTINCT removes duplicate customer numbers from the list, and the literal “Yes” indicates that the customer has bought product of class B3.

    Now let’s put it all together. We need to join the first query to the other two. We must use a left outer join, because some customers don’t qualify for the last two queries. The HasActiveOrder and BoughtB3 columns will come back “null” for customers who are not returned by the last two queries, so we use the COALESCE function to convert nulls to the value No.

    Here’s the query:

    select c.customerno, c.cusnam, 
           coalesce(act.HasActiveOrder,'No') as HasActiveOrder,
           coalesce(b3.BoughtB3,'No') as BoughtB3
      from customers as c
      left join 
              (select distinct o.customerno, 'Yes' as HasActiveOrder
                      from salesordh as o
                      where o.status <> '9') as act
        on c.customerno = act.customerno
      left join 
              (select distinct o.customerno, 'Yes' as BoughtB3
                 from salesordh as o
                 join salesordd as d
                   on o.orderno = d.orderno
                 join items as i
                   on d.itemno = i.itemno
                where i.class='B3') as b3
        on c.customerno = b3.customerno
     where c.type = 'A'
    

    And this is what the result might look like:

    CUSTOMERNO CUSNAM             HASACTIVEORDER  BOUGHTB3
     12345     Cal E. Phornya     Yes             No
     23456     Billy Rubin        No              Yes
     34567     Polly Unsaturated  Yes             Yes
     45678     Molly Coddle       Yes             No
    

    Isn’t it great how we can put SQL SELECT statements in places once reserved for table and view names?

    –Ted



                         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
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Help/Systems:  Event-driven job scheduling for UNIX, Linux, Windows & IBM i servers
    LANSA:  Transport your apps to a new dimension with RAMP. FREE Webinar!
    COMMON:  Join us at the annual 2010 conference, May 3 - 6, in Orlando, Florida

    IT Jungle Store Top Book Picks

    Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database 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
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    Chip Wars: List Price, $29.95

    Vendors Go Gaga for iPhone at Lotusphere LANSA Likes Its Chances as GS1 Item Alignment, GDSN Initiatives Advance

    Leave a Reply Cancel reply

Volume 10, Number 4 -- January 27, 2010
THIS ISSUE SPONSORED BY:

ProData Computer Services
WorksRight Software
Halcyon Software

Table of Contents

  • Creating Yes/No Fields in SQL Queries
  • A Few Excel Export to CSV Tips
  • Admin Alert: Speeding Up i5/OS Access Path Rebuilds

Content archive

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

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

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