Tables:
TABLE1: PROGRAM
Prgm_name | Company | People |
---|---|---|
A | X CO., Y CO. | JOHN |
B | M CO., N CO., O CO. | KRISH, BIKRAM, TOM |
TABLE2: COMPANY
Company_name | Program | People |
---|---|---|
X CO. | A | JOHN |
Y CO. | A | |
M CO. | B | |
N CO. | B | KRISH, TOM |
O CO. | B |
TABLE3: PEOPLE
Full_name | Company | program |
---|---|---|
JOHN | X CO. | A |
BIKRAM | B | |
KRISH | N CO. | B |
TOM | N CO. | B |
LYNN | N CO. |
create table Program
(
Prgm_name varchar(225),
Company varchar(500),
People varchar(500)
)
create table Companies
(
Company_name varchar(255),
Program varchar(500),
People varchar(500)
)
create table People
(
Full_name varchar(225),
Company varchar(500),
program varchar(500)
)
insert into program values ('A', 'X CO., Y CO.', 'JOHN')
insert into program values ('B', 'M CO., N CO., O CO.', 'KRISH, BIKRAM, TOM')
insert into Companies values ('X CO.','A','JOHN')
insert into Companies values ('Y CO.','A','')
insert into Companies values ('M CO.','B','')
insert into Companies values ('N CO.','B','KRISH, TOM')
insert into Companies values ('O CO.','B','')
insert into People values ('JOHN','X CO.','A')
insert into People values ('BIKRAM','','B')
insert into People values ('KRISH','N CO.','B')
insert into People values ('TOM','N CO.','B')
insert into People values ('LYNN','N CO.','')
SELECT * FROM Program
SELECT * FROM Companies
SELECT * FROM people
EXPECTED RESULT: Based on the above 3 tables I want the normalized PROGRAM table like the below:
Prgm_name | Company | People |
---|---|---|
A | X CO. | JOHN |
A | Y CO. | |
B | N CO. | KRISH |
B | BIKRAM | |
B | N CO. | TOM |
B | M CO. | |
B | O CO. |
To get this result I tried to write some script but still not getting the answer. The script is as follows:
select * into #program from
(
select a.PRGM_NAME as PRGM_NAME_P, a.COMPANY AS COMPANY_P, a.PEOPLE AS PEOPLE_P, c.Full_name AS FULL_NAME_PP,c.Company AS COMPANY_PP,c.program AS PROGRAM_PP from
(
select distinct b.PRGM_NAME, x.COMPANY, Y.PEOPLE
from
(SELECT PRGM_NAME, COMPANY, PEOPLE FROM Program) b
cross apply(select trim(value) from string_split(b.COMPANY, ',')) x(COMPANY) CROSS APPLY (select trim(value) from string_split(b.PEOPLE, ',')) Y(PEOPLE)
)a
left join
people c
on a.Prgm_name = c.program AND A.COMPANY = C.Company
)d
SELECT
PRGM_NAME_P,COMPANY_P AS COMPANY_P_Y, PEOPLE_P, FULL_NAME_PP, COMPANY_PP,PROGRAM_PP
,CASE
WHEN COMPANY_P <> '' AND COMPANY_PP = '' THEN FULL_NAME_PP
WHEN COMPANY_P <> COMPANY_PP THEN ''
ELSE FULL_NAME_PP
END FULL_NAME_Y
, ROW_NUMBER () OVER(PARTITION BY PRGM_NAME_P, PEOPLE_P ORDER BY COMPANY_PP DESC ) AS RANK1
FROM #program
I am not able to do more than this. it would be a great help if anyone guide me.