I want to insert new records in table Prices(Date, ID1, Price) based on records in table Headers (Date, ID1, ID2). If there exists a record in table Headers where its ID2 equals to ID1 of another record in this table, then a new record must be inserted in table Prices where its Date and ID1 is taken from table Headers, and its Price equals to the Price of the record with ID1 already in this table. For example:
Input:
Table Headers:
Date ID1 ID2
-------------------------------
2013.08.10 100 200
2013.08.10 300 100
2013.08.10 400 100
2013.08.11 200 500
2013.08.11 500 200
2013.08.11 600 200
Table Prices:
Date ID1 Price
---------------------------
2013.08.10 100 500
2013.08.11 200 1200
The output must include new records inserted in Prices for ID1=300 and ID1=400 with Price=500 (since the records with ID1= 300 and ID1=400 have ID2=100 which corresponds to the record with ID1=100). Similarly for the records with ID1=500 and ID1=600 new records must be inserted in table Prices based on the price of ID2=200:
Date ID1 Price
--------------------------------
2013.08.10 100 500
2013.08.10 300 500
2013.08.10 400 500
2013.08.11 200 1200
2013.08.11 500 1200
2013.08.11 600 1200
Pricestable or (2) the rows that must be inserted into it? Because, if it is the latter, you will have duplicateID1s, since100and200are already inPrices.