SQL错误消息 - '''附近的语法不正确

时间:2017-10-13 20:31:05

标签: sql-server

我收到此错误消息

  

“'附近的语法不正确')'。”

来自sum(PayHours)行,我会对如何解决这个问题给予一些指导。我被卡住了。

来自以下代码的

[#PremierThree]是一个即时创建的临时表,之后是

SELECT * 
FROM [#PremierThree]
PIVOT 
    (SUM(PayHours)
     FOR [Pay_ClassHrs] IN ([PBS_Hrs], [POT_Hrs], [PED_Hrs], [POR_Hrs],   [Agency_Hrs], [PWH_Hrs], [Sick_Hrs], [PTO_Hrs], [Holiday_Hrs], [PJB_Hrs], [Bugeted_Work_Hrs], [Bugeted_Benefit_Hrs])
    ) AS pvt1
PIVOT 
    (SUM(PayAmount)
     FOR [Pay_Class$] in ([PBS_$], [POT_$], [PED_$], [POR_$], [Agency_$], [PWH_$], [Sick_$], [PTO_$], [Holiday_$], [PJB_$], [PNH_$],[Bugeted_Work_$], [Bugeted_Benefit_$])
    ) AS pvt2
ORDER BY
    [Facility], CAST([Job_Code] AS INT)

1 个答案:

答案 0 :(得分:1)

数据透视查询在T-SQL中不包含2个或更多个数据透视子句,请参阅FROM - Using PIVOT and UNPIVOT

您正在尝试将小时和美元输出为“2列集”,坦率地说,我发现枢轴的T-SQL实现对此有太大的限制。您可以尝试使用unpivot和pivot一起获得所需效果的方法(example here)。

就个人而言,我会选择更传统的数据透视查询方式,只需根据需要使用GROUP BYcase expressions(在将数据库添加到SQL之前我们总是必须使用的样式)。例如(但我把列别名留给你)

SELECT
      [Facility]
    , [Job_Code]

    , SUM(CASE WHEN Pay_ClassHrs = 'PBS_Hrs' THEN Pay_ClassHrs END)             AS x
    , SUM(CASE WHEN Pay_ClassHrs = 'POT_Hrs' THEN Pay_ClassHrs END)             AS x
    , SUM(CASE WHEN Pay_ClassHrs = 'PED_Hrs' THEN Pay_ClassHrs END)             AS x
    , SUM(CASE WHEN Pay_ClassHrs = 'POR_Hrs' THEN Pay_ClassHrs END)             AS x
    , SUM(CASE WHEN Pay_ClassHrs = 'Agency_Hrs' THEN Pay_ClassHrs END)          AS x
    , SUM(CASE WHEN Pay_ClassHrs = 'PWH_Hrs' THEN Pay_ClassHrs END)             AS x
    , SUM(CASE WHEN Pay_ClassHrs = 'Sick_Hrs' THEN Pay_ClassHrs END)            AS x
    , SUM(CASE WHEN Pay_ClassHrs = 'PTO_Hrs' THEN Pay_ClassHrs END)             AS x
    , SUM(CASE WHEN Pay_ClassHrs = 'Holiday_Hrs' THEN Pay_ClassHrs END)         AS x
    , SUM(CASE WHEN Pay_ClassHrs = 'PJB_Hrs' THEN Pay_ClassHrs END)             AS x
    , SUM(CASE WHEN Pay_ClassHrs = 'Bugeted_Work_Hrs' THEN Pay_ClassHrs END)    AS x
    , SUM(CASE WHEN Pay_ClassHrs = 'Bugeted_Benefit_Hrs' THEN Pay_ClassHrs END) AS x

    , SUM(CASE WHEN Pay_Class$ = 'POT_$' THEN Pay_Class$ END)                   AS x
    , SUM(CASE WHEN Pay_Class$ = 'PED_$' THEN Pay_Class$ END)                   AS x
    , SUM(CASE WHEN Pay_Class$ = 'POR_$' THEN Pay_Class$ END)                   AS x
    , SUM(CASE WHEN Pay_Class$ = 'Agency_$' THEN Pay_Class$ END)                AS x
    , SUM(CASE WHEN Pay_Class$ = 'PWH_$' THEN Pay_Class$ END)                   AS x
    , SUM(CASE WHEN Pay_Class$ = 'Sick_$' THEN Pay_Class$ END)                  AS x
    , SUM(CASE WHEN Pay_Class$ = 'PTO_$' THEN Pay_Class$ END)                   AS x
    , SUM(CASE WHEN Pay_Class$ = 'Holiday_$' THEN Pay_Class$ END)               AS x
    , SUM(CASE WHEN Pay_Class$ = 'PJB_$' THEN Pay_Class$ END)                   AS x
    , SUM(CASE WHEN Pay_Class$ = 'PNH_$' THEN Pay_Class$ END)                   AS x
    , SUM(CASE WHEN Pay_Class$ = 'Bugeted_Work_$' THEN Pay_Class$ END)          AS x
    , SUM(CASE WHEN Pay_Class$ = 'Bugeted_Benefit_$' THEN Pay_Class$ END)       AS x

FROM [#PremierThree]
GROUP BY
      [Facility]
    , [Job_Code]

<强> UNTESTED!

如果上面的方法看起来太老了,那么对你来说就是罗嗦。尝试使用连接组合2个枢轴,下面我假设它需要一个full outer join但是如果两个子查询产生相同的行(很可能在一起,它可能适用于更简单的inner join使用该临时表的情况)。

SELECT
       COALESCE(p1.Facility,p2.Facility) Facility
     , COALESCE(p1.Job_Code,p2.Job_Code) Job_Code
     , [PBS_Hrs], [POT_Hrs], [PED_Hrs], [POR_Hrs],   [Agency_Hrs], [PWH_Hrs], [Sick_Hrs], [PTO_Hrs], [Holiday_Hrs], [PJB_Hrs], [Bugeted_Work_Hrs], [Bugeted_Benefit_Hrs]
     , [PBS_$], [POT_$], [PED_$], [POR_$], [Agency_$], [PWH_$], [Sick_$], [PTO_$], [Holiday_$], [PJB_$], [PNH_$],[Bugeted_Work_$], [Bugeted_Benefit_$]
FROM (
      SELECT
            [Facility]
          , [Job_Code]
          , [PBS_Hrs], [POT_Hrs], [PED_Hrs], [POR_Hrs],   [Agency_Hrs], [PWH_Hrs], [Sick_Hrs], [PTO_Hrs], [Holiday_Hrs], [PJB_Hrs], [Bugeted_Work_Hrs], [Bugeted_Benefit_Hrs]
      FROM [#PremierThree]
      PIVOT 
          (SUM(PayHours)
           FOR [Pay_ClassHrs] IN ([PBS_Hrs], [POT_Hrs], [PED_Hrs], [POR_Hrs],   [Agency_Hrs], [PWH_Hrs], [Sick_Hrs], [PTO_Hrs], [Holiday_Hrs], [PJB_Hrs], [Bugeted_Work_Hrs], [Bugeted_Benefit_Hrs])
          ) AS pvt1
      ) AS p1
FULL OUTER JOIN (
      SELECT
            [Facility]
          , [Job_Code]
          , [PBS_$], [POT_$], [PED_$], [POR_$], [Agency_$], [PWH_$], [Sick_$], [PTO_$], [Holiday_$], [PJB_$], [PNH_$],[Bugeted_Work_$], [Bugeted_Benefit_$]
      PIVOT 
          (SUM(PayAmount)
           FOR [Pay_Class$] in ([PBS_$], [POT_$], [PED_$], [POR_$], [Agency_$], [PWH_$], [Sick_$], [PTO_$], [Holiday_$], [PJB_$], [PNH_$],[Bugeted_Work_$], [Bugeted_Benefit_$])
          ) AS pvt2
      ) AS p2
             ON p1.Facility = p2.Facility AND p1.Job_Code = p2.Job_Code
ORDER BY
    [Facility], CAST([Job_Code] AS INT)
;

对于T-SQL:就个人而言,即使使用连接方法,我也看不到使用pivot时开始变得复杂的任何优势。