使用联合查询的多列

时间:2013-09-13 03:17:35

标签: sql sql-server union

我想从具有相同字段的2个表的单个联合查询中创建2列数据。我有2个表,每个表中都有“Utilizations”字段。

我尝试了以下查询但是我收到了一个错误。

    select Utilizations as "Utilizations A", Utilizations as "Utilizations B" from (select Utilizations as A, 0 as B from TableA union all select 0 as A, Utilizations as B from TableB)

1 个答案:

答案 0 :(得分:1)

首先,您需要为子查询设置别名,其次,您需要将外部查询中的列称为A和B,而不是实用程序:

select  A as "Utilizations A", 
        B as "Utilizations B" 
from 
  (select  Utilizations as A, 
           0 as B 
   from TableA 
   union all 
   select 0 as A, 
          Utilizations as B 
   from TableB
  )AS t