2017-06-15 271 views
1

我試圖轉換成列,我不斷遇到錯誤。我不知道我做錯了什麼。我嘗試了下面的代碼,但我不斷得到「FOR」和括號中的第一個單詞下的紅色波浪線。這裏是我的代碼:將列轉換成行,反之亦然

select d.City,d.Geographic_Region_Name, d.Site_Type 
from Site_Profile as d 
pivot 
(City for Geographic_Region_Name in (City,Geographic_Region_Name,site_type) as pivotable; 

回答

2

Pivot是用於車削集合行成列。從documentation

SELECT <non-pivoted column>, 
    [first pivoted column] AS <column name>, 
    [second pivoted column] AS <column name>, 
    ... 
    [last pivoted column] AS <column name> FROM 
    (<SELECT query that produces the data>) 
    AS <alias for the source query> PIVOT ( 

相關線路:

<aggregation function>(<column being aggregated>)

,其餘

FOR [<column that contains the values that will become column headers>] 
    IN ([first pivoted column], [second pivoted column], 
    ... [last pivoted column]) ) AS <alias for the pivot table> 
    <optional ORDER BY clause>; 

你需要使用一個聚合函數(SUMCOUNTMAX,MIN等)在FOR之前。

+0

不知道更多關於你的數據是什麼樣的以及你想要實現什麼,我不能確定你應該使用哪個函數。 – pcdev

+0

另請參閱[此問題](https://stackoverflow.com/questions/15931607/convert-rows-to-columns-using-pivot-in-sql-server) – pcdev

+0

非常感謝您的回覆,pcdev。非常感激 – user2089542

相關問題