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 に答える