Home > Ask the AS/400 Experts > DB2/400 and DB2 UDB Questions & Answers > Uniquely matching two fields in an SQL table to fields in a different table
Ask The iSeries 400 Expert: Questions & Answers
EMAIL THIS

Uniquely matching two fields in an SQL table to fields in a different table

Kent Milligan EXPERT RESPONSE FROM: Kent Milligan

Pose a Question
Other iSeries 400 Categories
Meet all iSeries 400 Experts
Become an Expert for this site


iSeries news and advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 09 October 2008
I am new to SQL and doing SQL by command on the AS/400. I need to make two fields in one table and uniquely match two fields in another table. If I run the following, I get exactly the records I need:

SELECT DISTINCT APPART, APSEQ# FROM METHDO WHERE APODES = 'EXTERNAL DRYING'

the result is

 Part                  Seq 
 Number 
 BLAU1003BA             30 
 BLAU1006AA             30 
 BLAU1004BA             30 
 BLAU1002BA             40 

but when I try to do both tables with this SELECT AOPART, AOSEQ# FROM CSTDAT/METHDR WHERE METHDR.AOPART IN ( SELECT METHDO.APPART FROM METHDO WHERE APO DES = 'EXTERNAL DRYING ') AND
METHDR.AOSEQ# IN (SELECT METHDO.APSEQ# FROM METHDO WHERE APODES = 'EXTERNAL DRYING ') AND AODEPT = '001OS'

I get this:

 Part                  Seq 
 Number 
 BLAU1003BA             30 
 BLAU1006AA             30 
 BLAU1004BA             30 
 BLAU1002BA             30 
 BLAU1002BA             40 

Notice the BLAU1002BA occurs twice. What I believe is happening is that I am getting the part at every possible occurrence of the sequence that my condition exists.

How do I get the unique value and what would be the update statement?


>

Here's an example of the UPDATE statement that you're trying to run:

UPDATE methdo SET apodes = 
(SELECT R.apodes FROM methdo O INNER JOIN methdr R 
    O.AOPART = R.AOPART and O.AOSEQ# = R.AOSEQ#) 
WHERE EXISTS 
(SELECT 1 FROM methdr R2 
    methdo.AOPART = R2.AOPART and methdo.AOSEQ# = R2.AOSEQ#) 


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
DB2/400 and DB2 UDB
When is the YES option for 'reuse deleted files' function the best choice?
Monitoring members 'stuck' within a physical file on an EDI system
Developing tables in a parent-child relationship in DB2
SQL server error message -321
Creating a host variable of the 'where in' statement in SQL
Choose which column names are returned via ODBC when working with DB2 files
Allow access to data from a stored procedure result set using COBOL or RPG
Access path and an open data path differences
Convert a numeric physical file to a character in SQL without leading zeros
Inserting data from a CTE into a file in SQL

DB2 UDB (universal databases)
Oracle boasts 11g on SPARC is faster than IBM DB2 on Power 595
When is the YES option for 'reuse deleted files' function the best choice?
Monitoring members 'stuck' within a physical file on an EDI system
Developing tables in a parent-child relationship in DB2
SQL server error message -321
Creating a host variable of the 'where in' statement in SQL
Choose which column names are returned via ODBC when working with DB2 files
Access path and an open data path differences
Database performance comparisons on IBM i
Implement variables in SQL when creating an alias
DB2 UDB (universal databases) Research

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



iSeries Networking - Printing, Remote Access, TCP/IP
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts