Hi All,
Please help me with an issue I am facing with joining tables....been trying my best to sort it out since last couple of days but to no avail. Surely I am missing out something very trivial which would be good to if you experts can point it out.
Query for the sample data with the current result (which is wrong) is as below
create table #Temp1 (property_id int ,InvestorAccId int ,Ownership_Percent decimal ) insert into #Temp1 (property_id,InvestorAccId,Ownership_Percent) values ('1','51000','100.00') insert into #Temp1 (property_id,InvestorAccId,Ownership_Percent) values ('430','51000','50.00') insert into #Temp1 (property_id,InvestorAccId,Ownership_Percent) values ('430','71000','50.00') select * from #Temp1 create table #Temp2 (InvestorAccId int ,Portfolio_id int ) INSERT INTO #Temp2 (InvestorAccId,Portfolio_id) Values ('51000','1600') INSERT INTO #Temp2 (InvestorAccId,Portfolio_id) Values ('51000','2040') INSERT INTO #Temp2 (InvestorAccId,Portfolio_id) Values ('71000','2040') SELECT * FROM #Temp2 CREATE TABLE #Temp3 (Portfolio_id int ,asset_id int ,Name varchar(50) ) INSERT INTO #Temp3 (Portfolio_id,asset_id,Name) values ('1600','1490','225 Chroley Road') INSERT INTO #Temp3 (Portfolio_id,asset_id,Name) values ('2040','1920','14 Market Street') SELECT * FROM #Temp3 CREATE TABLE #Temp4 (Portfolio_id int ,current_value money) INSERT INTO #Temp4 (Portfolio_id,current_value) values ('1600','125000.00') INSERT INTO #Temp4 (Portfolio_id,current_value) values ('2040','163000.00') SELECT * FROM #Temp4 SELECT #Temp1.InvestorAccId,#Temp1.Ownership_Percent, #Temp2.Portfolio_id,#Temp3.Name,#Temp4.current_value FROM #Temp1 INNER JOIN #Temp2 on #Temp1.InvestorAccId = #Temp2.InvestorAccId INNER JOIN #Temp3 ON #Temp3.Portfolio_id = #Temp2.Portfolio_id INNER JOIN #Temp4 on #Temp4.Portfolio_id = #Temp2.Portfolio_id drop table #Temp1 drop table #Temp2 drop table #Temp3 drop table #Temp4
Expected Result is (out of 5 rows that the above query returns, only 3 are expected as shown below)
InvestorAccId | Ownership_Percent | Portfolio_id | Name | current_value |
51000 | 100 | 1600 | 225 Chroley Road | 125000 |
51000 | 50 | 2040 | 14 Market Street | 163000 |
71000 | 50 | 2040 | 14 Market Street | 163000 |