2017-07-25 44 views
0

說,我有6個臨時表存儲爲如下(這3個樣本),我會形成他們到一個單一的表,以串聯(標題日期,通訊,價格)。梳理臨時表的系列

Com A 
Date      Price 
2015-05-01 00:00:00.000 34.25 
2015-05-02 00:00:00.000 35.20 
2015-05-03 00:00:00.000 36.70 
2015-05-04 00:00:00.000 32.37 
2015-05-05 00:00:00.000 32.40 
2015-05-06 00:00:00.000 32.20 


Com B 
Date      Price 
2015-05-07 00:00:00.000 54.29 
2015-05-08 00:00:00.000 54.50 
2015-05-09 00:00:00.000 56.21 
2015-05-10 00:00:00.000 56.70 
2015-05-11 00:00:00.000 58.20 

Com C 
Date      Price 
2015-05-12 00:00:00.000 34.29 
2015-05-13 00:00:00.000 24.50 
2015-05-14 00:00:00.000 76.21 
2015-05-15 00:00:00.000 36.70 
2015-05-16 00:00:00.000 48.20 

輸出的樣子,我想將其存儲爲另一個臨時表中供以後合併:根據您的樣本數據

Date      Com Price 
2015-05-01 00:00:00.000 A  34.25 
2015-05-02 00:00:00.000 A  35.20 
2015-05-03 00:00:00.000 A  36.70 
2015-05-04 00:00:00.000 A  32.37 
2015-05-05 00:00:00.000 A  32.40 
2015-05-06 00:00:00.000 A  32.20 
2015-05-07 00:00:00.000 B  54.29 
2015-05-08 00:00:00.000 B  54.50 
2015-05-09 00:00:00.000 B  56.21 
2015-05-10 00:00:00.000 B  56.70 
2015-05-11 00:00:00.000 B  58.20 
2015-05-12 00:00:00.000 C  34.29 
2015-05-13 00:00:00.000 C  24.50 
2015-05-14 00:00:00.000 C  76.21 
2015-05-15 00:00:00.000 C  36.70 
2015-05-16 00:00:00.000 C  48.20 

回答

2

似乎是一個對我來說簡單的union all

SELECT [Date], 'A' as Com, Price 
FROM [Com A] 

UNION ALL 

SELECT [Date], 'B' as Com, Price 
FROM [Com B] 

UNION ALL 

SELECT [Date], 'C' as Com, Price 
FROM [Com C] 
1

Select Date,'A' AS Com,Price from [COM A] 
UNION ALL 
Select Date,'B' AS Com,Price from [COM B] 
UNION ALL 
Select Date,'C' AS Com,Price from [COM C]