TechTalk: Joining Files in SQL

SQL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

From: Philip Tsoiasue To: All

I find one difference between SQL and Query to be a particular problem in SQL. Query has the ability to return records that do not necessarily have a matched record in the secondary file. For example, I have a master file that allows users to specify a code in one of the fields. If the code is specified, I would like to print the description of the code (stored in a secondary file). Query returns all records in the primary file, but SQL returns only the records with codes because it cannot find the blank code in the secondary file. Any alternatives?

From: Doug Payton To: Philip Tsoiasue

Here's the only alternative I know of:

 SELECT mcode, 'Not Found' FROM master WHERE mcode not in (SELECT scode FROM secfile) 

This prints all codes in the master file that are not in the secondary file. Returning to your problem, I don't see a way to do this in one SQL statement. Maybe you could merge two SELECT statements with the UNION clause.

From: Sharon Cannon To: Philip Tsoiasue

After reading Doug's message, I think I see an easy way to accomplish this by using the following SQL statement:

 SELECT mcode, desc FROM master, secfile WHERE mcode = scode UNION ALL SELECT mcode, 'Not Found' FROM master WHERE mcode not in (SELECT scode FROM secfile WHERE scode = master.mcode) 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: