2013-08-16 120 views
0

我有這樣的數據:從行創建列名稱?

name  | coulmnnuber 
Newyork | 1 
washington| 2 
denmark | 3 
Holand | 4 

數據應該是這樣的:

1   2   3   4 
New york Washington denmark Holand 
+0

是否張貼問題之前,沒有人搜索? – SQLMason

回答

4

可以使用聚合函數與CASE表達式的數據行轉換爲列:

select 
    max(case when coulmnnuber = 1 then name end) [1], 
    max(case when coulmnnuber = 2 then name end) [2], 
    max(case when coulmnnuber = 3 then name end) [3], 
    max(case when coulmnnuber = 4 then name end) [4]    
from yourtable; 

請參閱SQL Fiddle with Demo

或者你可以使用旋轉功能:

select [1], [2], [3], [4] 
from yourtable 
pivot 
(
    max(name) 
    for coulmnnuber in ([1], [2], [3], [4]) 
) piv; 

SQL Fiddle with Demo

+0

這是我唯一能說的優秀。沒有詞。完善 – user2690007