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

时间:2016-02-05 02:21:58

标签: sql sql-server-2014

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

  

Msg 207,Level 16,State 1,Line 2
  列名称“TotalHours”无效。

     

Msg 207,Level 16,State 1,Line 2
  列名称“ClientServiceHours”无效。

     

Msg 207,Level 16,State 1,Line 2
  列名称“ClientPRDHours”无效。

     

Msg 207,Level 16,State 1,Line 3
  列名称'OtherReportableTime'无效。

     

Msg 207,Level 16,State 1,Line 3
  列名称'WeekendHours'无效。

     

Msg 207,Level 16,State 1,Line 3
  列名称“FlightsPerWeek”无效。

     

Msg 207,Level 16,State 1,Line 4
  列名称“利用率”无效。

     

Msg 207,Level 16,State 1,Line 4
  列名称“CEDHours”无效。

以下是我正在运行的代码:

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

以下是我正在使用的表格布局以及当前表格中的少量数据:

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数据库上运行它。

非常感谢任何帮助。

由于 NBW

1 个答案:

答案 0 :(得分:0)

预期结果是什么?

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

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