0
SELECT     
   TOP (100) PERCENT 
   dbo.bARCM.CustGroup, dbo.bARCM.Customer, 
   CASE WHEN udJobType IN ('Scheduled Maintenance', 'Unscheduled Emergency', 
                      'Unscheduled Call Out') THEN 'Maintenance' 
        WHEN udJobType IN ('Scheduled Special Projects', 'UPS Internal Capital Exp') 
                      THEN 'Capital' 
        WHEN udJobType LIKE '%Turnaround%' THEN 'T/A' 
   END AS JobType, 
   CASE WHEN Factor = 1.0 THEN 'ST' 
        WHEN Factor = 1.5 THEN 'OT' 
        WHEN Factor = 2.0 THEN 'OT' 
   END AS STOT, 
   SUM(dbo.bJBID.Hours) AS Hours,
   DATEADD(MONTH, DATEDIFF(MONTH, 0, dbo.bJBID.JCDate), 0) as SortMonth
FROM         
   dbo.bJBID 
INNER JOIN
   dbo.bJBIN ON dbo.bJBID.JBCo = dbo.bJBIN.JBCo AND dbo.bJBID.BillMonth = dbo.bJBIN.BillMonth AND dbo.bJBID.BillNumber = dbo.bJBIN.BillNumber 
INNER JOIN
   dbo.bARCM 
INNER JOIN
   dbo.bJCCM ON dbo.bARCM.CustGroup = dbo.bJCCM.CustGroup AND dbo.bARCM.Customer = dbo.bJCCM.Customer 
INNER JOIN
   dbo.JCJMPM ON dbo.bJCCM.JCCo = dbo.JCJMPM.JCCo AND dbo.bJCCM.Contract = dbo.JCJMPM.Contract ON dbo.bJBIN.JBCo = dbo.JCJMPM.JCCo AND 
                      dbo.bJBIN.Contract = dbo.JCJMPM.Contract 
INNER JOIN
    dbo.bJCCT ON dbo.bJBID.CostType = dbo.bJCCT.CostType AND dbo.bJBID.PhaseGroup = dbo.bJCCT.PhaseGroup 
INNER JOIN
    dbo.budAcctMonths ON dbo.budAcctMonths.Month = dbo.bJBIN.BillMonth
WHERE     
    (dbo.bJCCM.JCCo = 1) 
    AND (dbo.bJBID.CostType IN (1, 41, 42, 43, 44, 45, 46)) 
    AND (dbo.bJBID.CostTypeCategory = 'L') 
    AND (dbo.JCJMPM.udPlantLocation LIKE 'Deer%') 
    AND (dbo.bARCM.Name LIKE 'Dow%' OR dbo.bARCM.Name LIKE 'Rohm%')
GROUP BY 
    dbo.bARCM.CustGroup, dbo.bARCM.Customer, 
    dbo.JCJMPM.udJobType, dbo.bJBID.Factor, dbo.SortMonth
HAVING      
    (dbo.bARCM.CustGroup = 1) AND (SUM(dbo.bJBID.Hours) <> 0)

当我执行此查询时,我得到

无法绑定多部分标识符“dbo.SortMonth”

错误信息。我是 SQL 新手,需要一些帮助。

4

1 回答 1

3

您正在为以下内容SELECT分配别名,但除非在子查询中命名,否则您不能使用别名。SortMonthDATEADD(MONTH, DATEDIFF(MONTH, 0, dbo.bJBID.JCDate), 0)GROUP BY

您需要将代码更改为:

GROUP BY dbo.bARCM.CustGroup, 
    dbo.bARCM.Customer, 
    dbo.JCJMPM.udJobType, 
    dbo.bJBID.Factor,
    DATEADD(MONTH, DATEDIFF(MONTH, 0, dbo.bJBID.JCDate), 0)  -- use the DATEADD code here not the alias
于 2013-02-19T15:44:52.963 回答