在 SQL 的 Unpivot 查询中接收到无效的列名

Receiving Invalid Column names in Unpivot Query in SQL

当我尝试 运行 在查询分析器中执行以下查询时,我收到消息:

Msg 207, Level 16, State 1, Line 2
Invalid column name 'TotalHours'.

Msg 207, Level 16, State 1, Line 2
Invalid column name 'ClientServiceHours'.

Msg 207, Level 16, State 1, Line 2
Invalid column name 'ClientPRDHours'.

Msg 207, Level 16, State 1, Line 3
Invalid column name 'OtherReportableTime'.

Msg 207, Level 16, State 1, Line 3
Invalid column name 'WeekendHours'.

Msg 207, Level 16, State 1, Line 3
Invalid column name 'FlightsPerWeek'.

Msg 207, Level 16, State 1, Line 4
Invalid column name 'Utilization'.

Msg 207, Level 16, State 1, Line 4
Invalid column name 'CEDHours'.

这是我的代码 运行ning:

SELECT 
    THID AS 'ThresholdID', ThresholdID,
    TotalHours, ClientServiceHours, ClientPRDHours,
    OtherReportableTime, WeekendHours, FlightsPerWeek,
    HotelNightsPerWeek, Utilization, CEDHours,
    b.THX AS 'Threshold', ThresholdID AS 'ThresholdValue'
FROM 
    (SELECT 
         THID,
         TotalHours, ClientServiceHours, ClientPRDHours,
         OtherReportableTime, WeekendHours, FlightsPerWeek,
         HotelNightsPerWeek, Utilization, CEDHours
     FROM 
         [dbo].[MyTable]) AS a
UNPIVOT
(
     ThresholdID FOR THX IN (
        TotalHours, ClientServiceHours, ClientPRDHours,
        OtherReportableTime, WeekendHours, FlightsPerWeek,
        Utilization, CEDHours)
) AS b
ORDER BY THID

这是我正在使用的 table 布局以及当前 table 中的少量数据:

THRESHOLDID THID    TOTALHOURS  CLIENTSERVICEHOURS  CLIENTPRDHOURS  OTHERREPORTABLETIME WEEKENDHOURS    FLIGHTSPERWEEK  HOTELNIGHTSPERWEEK  UTILIZATION CEDHOURS
0   0   0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
1   1   55.00   0.00    0.00    0.00    0.10    5.00    5.00    90.00   0.00
2   2   55.00   0.00    0.00    0.00    0.10    5.00    5.00    90.00   0.00
3   3   55.00   0.00    0.00    0.00    0.10    5.00    5.00    90.00   0.00
4   4   55.00   0.00    0.00    0.00    0.10    5.00    5.00    90.00   0.00
5   5   55.00   0.00    0.00    0.00    0.10    5.00    5.00    80.00   0.00
6   6   55.00   0.00    0.00    0.00    0.10    5.00    5.00    70.00   0.00
7   7   55.00   0.00    0.00    0.00    0.10    5.00    5.00    70.00   0.00
8   8   41.67   39.93   7.90    0.00    0.31    1.24    3.00    1.00    0.00
9   9   45.49   39.70   12.51   0.00    0.88    1.10    3.00    0.99    0.00

如您所见,列名是正确的。我想知道是否有人知道为什么我会收到列名无效的错误消息。我在 SQL Server 2014 数据库上 运行ning 这个。

非常感谢任何帮助。

谢谢 nbw

预期的结果是什么?

您的代码仅适用于以下语法:

select  THID as 'ThresholdID',
        ThresholdID TotalHours,
b.THX AS 'Threshold', ThresholdID AS 'ThresholdValue'
from    (
            select  
                    TotalHours,
                    ClientServiceHours,
                    ClientPRDHours,
                    OtherReportableTime,
                    WeekendHours,
                    FlightsPerWeek,
                    HotelNightsPerWeek,
                    Utilization,
                    CEDHours,
                    THID
            from    [dbo].[MyTable]
        ) as a
UNPIVOT(ThresholdID for THX in(
    TotalHours,
    ClientServiceHours,
    ClientPRDHours,
    OtherReportableTime,
    WeekendHours,
    FlightsPerWeek,
    Utilization,
    CEDHours
)
) as b
order by    THID