2017-09-08 42 views
1

我有兩個表有類似的信息:
1st Table For Estimation Data :
如何合併兩個表爲一個輸出數據

EstChargeCode | EstAmount 
------------- | ------------- 
CNFS0001  | 43,250,000.00 
CNIH0001  | 0.00 
CNIH0001  | 2,625,000.00 
CNIP0001  | 4,500,000.00 
CNIP0005  | 2,250,000.00 
CNOH0001  | 20,484,690.00 
CNOP0001  | 0.00 

2nd Table for Actual Data :

ActChargeCode | ActAmount 
------------- | ------------- 
CNFS0001  | 39,950,000.00 
CNIH0001  | 1,300,000.00 
CNIH0001  | 950,000.00 
CNIH0001  | -950,000.00 
CNIH0001  | 950,000.00 
CNIP0001  | 4,500,000.00 
CNIP0005  | 2,250,005.00 
CNOH0001  | 20,484,690.00 
CNOP0001  | 3,300,000.00 

如果使用聯盟所有合併表從上面然後像這樣的結果

ChargeCode | EstAmount  | ActAmount 
---------- | ------------- | ------------- 
CNFS0001 | 43,250,000.00 | ------------- 
CNIH0001 | 0.00   | ------------- 
CNIH0001 | 2,625,000.00 | ------------- 
CNIP0001 | 4,500,000.00 | ------------- 
CNIP0005 | 2,250,000.00 | ------------- 
CNOH0001 | 20,484,690.00 | ------------- 
CNOP0001 | 0.00   | ------------- 
CNFS0001 | ------------- | 39,950,000.00 
CNIH0001 | ------------- | 1,300,000.00 
CNIH0001 | ------------- | 950,000.00 
CNIH0001 | ------------- | -950,000.00 
CNIH0001 | ------------- | 950,000.00 
CNIP0001 | ------------- | 4,500,000.00 
CNIP0005 | ------------- | 2,250,005.00 
CNOH0001 | ------------- | 20,484,690.00 
CNOP0001 | ------------- | 3,300,000.00 

我需要組中的兩個數據轉換成單一的結果數據是這樣

ChargeCode | EstAmount  | ActAmount 
---------- | ------------- | ------------- 
CNFS0001 | 43,250,000.00 | 39,950,000.00 
CNIH0001 | 0.00   | 1,300,000.00 
CNIH0001 | 0.00   | 950,000.00 
CNIH0001 | 0.00   | -950,000.00 
CNIH0001 | 0.00   | 950,000.00 
CNIH0001 | 2,625,000.00 | 0.00 
CNIP0001 | 4,500,000.00 | 4,500,000.00 
CNIP0005 | 2,250,000.00 | 2,250,005.00 
CNOH0001 | 20,484,690.00 | 20,484,690.00 
CNOP0001 | 0.00   | 3,300,000.00 

我不知道該怎麼處理了這一點。任何幫助將不勝感激!

+0

你只需要與'INNER JOIN' – Sami

回答

0
select a.EstChargeCode as ChargeCode , 
     a.EstAmount, 
     b.ActAmount 
from Estimation_Data as a 
LEFT JOIN Actual_Data as b 
ON a.ActChargeCode =b.ActChargeCode 
+0

@ankit什麼是錯的,我的回答你的加入表?檢查你的答案沒有這樣的Estimation_Data表 –

+0

真是一個ActChargeCode列? 'LEFT JOIN'? – Sami

+1

@Sami可能是有效的 - 我們不知道行是否保證存在於任何表中。估計本來可能沒有實際數額。實際的金額可以在沒有估計的情況下知道 - OP沒有具體說明。沒有猜測,唯一有效的選擇是完全外部聯接(就像哈桑的回答)。 – Bridge

1

你可以使用類似的代碼如下

select isnull(EstChargeCode ,ActChargeCode) as ChargeCode ,isnull(EstAmount,0) as 
    EstAmount , isnull(ActAmount,0) as ActAmount from Estimation full join Actual 
    on Estimation.EstChargeCode =Actual.ActChargeCode 
0

您可以使用JOIN達到的效果:

SELECT t2.ActAmount ChargeCode ,nvl(t1.EstAmount,0.0) EstAmount, nvl(t2.ActAmount,0.0) ActAmount 
FROM <Estimation Data table> t1 
RIGHT OUTER JOIN <Actual Data> t2 ON t1.EstChargeCode = t2.ActChargeCode ; 

更多的澄清,這裏有個解釋: What is the difference between "INNER JOIN" and "OUTER JOIN"?

+1

使用JOIN而不是Union –

0

您使用INNER JOIN如:

SELECT T1.EstChargeCode AS EstChargeCode, 
     T1.EstAmount AS EstAmount1, 
     T2.EstAmount AS EstAmount2 
FROM Table1 T1 INNER JOIN Table2 T2 ON T1.EstChargeCode = T2.ActChargeCode;