2017-02-24 25 views
1

我希望有此表,将每两个列的数据合并到1(Material1 +材料2,数量1 + Quantity2,时间1 +时间2)和另一行,以确定查询材质是否是材质1或材质2。合并列然后识别源列

输入数据:

enter image description here

所需的输出:

desired Output table

我曾尝试:

SELECT Material1 as Material 
FROM [test] 
UNION all 
SELECT Material2 
FROM [test] 

SELECT [Quantity1] as Quantity 
FROM [test] 
Union all 
Select Quantity2 
FROM [test] 
ORDER BY Quantity1 

,但它原来是两个表..

+0

欢迎SO。请看看[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 –

+0

您的“合并列”具有误导性。你想“转动”你的数据。 –

+0

耶我希望把两列的数据只是一个,不CONCAT ..误导你... –

回答

1

也许这样的事情?

select 'Material1' as Name 
     ,Material1 as Material 
     ,Quantity1 as Quantity 
     ,Time1 as [Time] 
from [test] 

union all 

select 'Material2' as Name 
     ,Material2 as Material 
     ,Quantity2 as Quantity 
     ,Time2 as [Time] 
from [test] 
+0

非常感谢! –