zoukankan      html  css  js  c++  java
  • SQL PIVOT

    可以使用 PIVOT 和 UNPIVOT 关系运算符将表值表达式更改为另一个表。PIVOT 通过将表达式某一列中的唯一值转换为输出中的多个列来旋转表值表达式,并在必要时对最终输出中所需的任何其余列值执行聚合。UNPIVOT 与 PIVOT 执行相反的操作,将表值表达式的列转换为列值。

    示例

    源数据

    DaysToManufacture          AverageCost

    0                          5.0885

    1                          223.88

    2                          359.1082

    4                          949.4105

    使用pivot行转列以得到以下的数据:

    Cost_Sorted_By_Production_Days    0         1         2           3       4       

    AverageCost                       5.0885    223.88    359.1082    NULL    949.4105

    -- Pivot table with one row and five columns
    SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days, 
    [0], [1], [2], [3], [4]
    FROM
    (SELECT DaysToManufacture, StandardCost 
        FROM Production.Product) AS SourceTable
    PIVOT
    (
    AVG(StandardCost)
    FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
    ) AS PivotTable

    详见:

    使用 PIVOT 和 UNPIVOT(MSDN)

  • 相关阅读:
    欧拉回路一个定理的证明
    NOIP2018 初赛数学第二题解析
    linux 减少Terminal路径的方法
    网络挖坑
    linux 记录
    河南游记 Day0

    NOI2018 Day 1 你的名字
    大佬的几行fastIO
    Codeforces 781B. Innokenty and a Football League
  • 原文地址:https://www.cnblogs.com/dataadapter/p/2586266.html
Copyright © 2011-2022 走看看