如何获得具有不同别名的两个表的并集

时间:2013-07-22 12:08:09

标签: sql-server-2008

我想获得联盟,但希望用不同的别名显示一列两次。 E.g

Select a as first from table1 
union 
select a as second from table1

首先显示为输出。

但我想展示第一和第二。如何在MSSQL中实现?

3 个答案:

答案 0 :(得分:2)

Union将两个查询的结果合并为一组列。没有办法让列名更改一半。我能想到的最接近的是

select a as first
, null as second 
from table1 

union all 

select null as first
, a as second 
from table1

答案 1 :(得分:1)

您将拥有union all

的所有行
Select a as first from table1 
union all 
select a as second from table1

答案 2 :(得分:1)

如果我做得对:

Select a as first,a as second from table1