【问题标题】:Pivoting columns into rows and vice versa将列旋转为行,反之亦然
【发布时间】:2017-06-16 13:14:04
【问题描述】:

我正在尝试转入列,但我一直遇到错误。我不知道我做错了什么。我尝试了下面的代码,但我一直在“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;

【问题讨论】:

    标签: sql tsql pivot


    【解决方案1】:

    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   (  
    

    相关线路:

    &lt;aggregation function&gt;(&lt;column being aggregated&gt;)

    其他的

        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>;
    

    您需要在FOR 之前使用聚合函数(SUMCOUNTMAXMIN 等)。

    【讨论】:

    • 如果不知道更多关于你的数据是什么样子以及你想要实现什么,我真的不能说你应该使用哪个函数。
    • 非常感谢您的回复,pcdev。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2010-12-03
    • 2011-10-08
    • 2013-03-15
    • 2019-09-19
    • 2013-12-29
    • 2011-09-22
    • 2012-05-14
    • 2020-03-01
    相关资源
    最近更新 更多