Link to home
Start Free TrialLog in
Avatar of Parth Verma
Parth VermaFlag for Australia

asked on

Insert data by joining tables

Hi All,

I am having below 3 tables

Table 1-
RJ_SONIC_STAGING

ITEM, SCAN_ID, STATE_ID, VENDOR
1234, 9876656, VA, 5643738
3453, 8374839, WA, 6382927
1283, 7352902, VA,
1234, 1722718, WA,

above is example of table records, For few records Vendor is NULL

Table 2-
CONFIG_TABLE

STATE_ID, VENDOR
VA, 432345
WA, 362819

Here i have some default value for STATE_ID

Table 3-
INCEPTION_TABLE

Table format is
ITEM, SCAN_ID, VENDOR

I want to insert into table 3, data from table 1.
But if VENDOR is NULL in table 1, it should take default value from TABLE 2 and put into TABLE 3.

How should i do it in oracle sql.
i dont want to hardcode the value, like
CASE if VA then 432345

cuz they can change Value in table 2 in future..
Avatar of Helena Marková
Helena Marková
Flag of Slovakia image

Maybe you can try something like this:

insert into INCEPTION_TABLE select
ITEM, SCAN_ID, VENDOR from
(select a.ITEM, a.SCAN_ID, a.VENDOR
from
select RJ_SONIC_STAGING a where a.VENDOR is not null
UNION
select a.ITEM, a.SCAN_ID, b.VENDOR
from RJ_SONIC_STAGING a,CONFIG_TABLE b
where a.STATE_ID=b.STATE_ID and a.VENDOR is null)
Avatar of Parth Verma

ASKER

thank you for the reply.
Let me rephrase my question. i am sorry for the trouble.

I want to insert only those records in table 3 where VENDOR is NULL in table 1.
and while inserting into table 3, i want to join the STATE_ID of table 1 with STATE_ID of Table 2 and fetch that record

basically something like this

Insert into INCEPTION_TABLE
(
ITEM,
SCAN_ID,
VENDOR)
select
ITEM,
SCAN_ID,
-- here i am not sure how to fetch data from table 2 and join with the STATE_ID
from RJ_SONIC_STAGING
where VENDOR IS NULL;
This ought to do it:

Insert into INCEPTION_TABLE
 select a.ITEM, a.SCAN_ID, b.VENDOR
from RJ_SONIC_STAGING a,CONFIG_TABLE b
where a.STATE_ID=b.STATE_ID and a.VENDOR is null;
"I am having below 3 tables"

Yes, speakers of English outside of India can understand what you mean by this sentence, since those are all English words, but that is distinctively "Indian English" word order that looks quite foreign to native speakers of English.

First, we use "am having" only for very temporary conditions.  For long-term, or ownership or (almost) permanent conditions, we say: "I have...".

Then the English word "below" is not an adjective, it is an adverb, so we never put it in a sentence where an adjective is expected.  We would say: "I have the three tables below:" or "I have three tables, described below:".

The last suggested solution from Helena looks pretty good to me, but I would write the last line like this:
where b.STATE_ID=a.STATE_ID and a.VENDOR is null;
so the "known" value is always to the right of the "=" sign, and the first table listed in the "from" clause is the "driving" table tht Oracle starts with.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.