如何将两个表合并为一个输出数据

时间:2017-09-08 09:50:43

标签: sql-server union

我有两张表格相似的表格:
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

我不知道如何解决这个问题。任何帮助将不胜感激!

4 个答案:

答案 0 :(得分: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 

答案 1 :(得分: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 

答案 2 :(得分: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"?

答案 3 :(得分: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;