2017-02-24 49 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

非常感謝! –