2017-06-15 261 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

相关问题