Question : Joining a portion of a table to itself

Experts,

My ultimate goal is to load my source data, which is now in a temporary table, into a main table in the DB.  I have figured out that only some of our new data ( which is history ) needs to be loaded to the main table ( as much of the information is already on there ).  In fact I know that only 86 more records need to be inserted into our main table from this temporary table I have set up.  To do this easily ( though it doesn't seem so easy now to me ), I have created another temporary table with just two key fields of the 86 records I want inserted into our main table.    Now the next step is to create a query to join my original temp table with the history data on it, with 13, 614 records, to my subset of that of only 86 records.  I am trying to use the 86 record table to drive this.  What I ultimately want is a set of 86 records with the two key fields from my small subset temp table and the matching information on the table it originally came from, the other temp table with over 13,000 records.  I am not getting the expected results.  I have saved both temp tables to spreadsheets to document my work.  If someone could please advise, I would be appreciative.

Scott
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
select 
tcc.rtsta,
'W',
ds.rtclas,
ds.description,
to_date('01/01/1994','mm/dd/yyyy'),
to_date('12/31/2078','mm/dd/yyyy'),
tcc.rthzgr
from tmp_class_code_history tcc
join tmp_dstctrtclasrtrdsc ds on tcc.rtclas=ds.rtclas
order by tcc.rtclas
Open in New Window Select All

Answer : Joining a portion of a table to itself

Can you not just do:

INSERT INTO main_table
SELECT tcc.rtsta,
  ...
FROM tmp_class_code_history tcc
JOIN tmp_dstctrtclasrtrdsc ds ON (tcc.rtclas=ds.rtclas)

>What I ultimately want is a set of 86 records with the two key fields from my small subset temp table >and the matching information on the table it originally came from, the other temp table with over 13,000 >records.  I am not getting the expected results.

Are you expecting to only have 86 records inserted?  Looking at your data, RTCLAS is not unique in tmp_class_code_history, so your query will insert more than 86 records.
Random Solutions  
 
programming4us programming4us