代码之家  ›  专栏  ›  技术社区  ›  Randy Minder

数据透视输出中的额外行

  •  1
  • Randy Minder  · 技术社区  · 7 年前

    我有一个简单的表,如下所示:

    enter image description here

    我正在执行以下查询:

    SELECT 
        DeviceId,
        [UCL],
        [LCL]
    FROM dbo.BreweryTemperatureSetpoint
    PIVOT
    (
        SUM(SetpointValue) FOR SetpointName IN ([UCL], [LCL])
    ) AS PVT
    

    输出如下所示:

    enter image description here

    我想要的是一行,如下所示:

    Prototype1, 24, 21.75
    

    关于Pivot,我不太了解,因为我本以为我的查询会产生所需的输出,但事实并非如此。

    1 回复  |  直到 7 年前
        1
  •  2
  •   John Cappelletti    7 年前

    将“源”中的字段限制为所需的字段。XAxis、YAxis和Value

    SELECT *
    FROM (
            Select DeviceId,SetpointValue,SetpointName
             FROM dbo.BreweryTemperatureSetpoint
         )  A
    PIVOT
    (
        SUM(SetpointValue) FOR SetpointName IN ([UCL], [LCL])
    ) AS PVT