Hi, I am struggling with a merge statement. I have a staging table with two names in it. I've simplified it for this but its
Create Table stg_partner_names
( t1_partner1 varchar(100)
,t2_partner2 varchar(100)
,t3_partner3 varchar(100)
, risk_score decimal (10,2))
insert into stg_partner_names (t1_partner1,t2_partner2,t3_partner3) VALUES( 'Bob','Rita', 'Sue')
so the hierarchy is t3 reports to t2 and t2 reports to t1
I want to populate a partner relationship table using these values to lookup the id for each of the names
create table partner_relationship (
id integer not null IDENTITY(1,1) PRIMARY KEY,
--partner_relationship_type_id integer null,
partner_from integer null,
partner_to integer null)
The ids for each partner are here, so I want to return the ids for each partner using the partner_name:
create table partner (
id integer not null IDENTITY(1,1) PRIMARY KEY,
uid integer null,
partner_name varchar(255) null)
insert into partner (partner_name) VALUES('Bob')
insert into partner (partner_name) VALUES('Rita')
insert into partner (partner_name) VALUES('Sue')
I would expect to see in partner_relationship once Ive inserted
id, partner_from partner_to
1 3 2
2 2 1
Do I need self joins , cte ? Would l it be better to do it as two separate inserts for each 'tier'?
Thanks for any help in advance