自连接问题 - 定义提取的时间段

Self Join Issue - Defining Extracted Time Periods

在下面的查询中,我提取了 Select 查询中的月份。然后我使用提取的月份执行多个自连接。我得到一个

Invalid column name 'mnth'

每次在自联接中引用该字段。我将在下面的查询中在哪里定义提取的月份?

SELECT   MONTH(frcst.InvDate) AS mnth
       , frcst.LineCode
       , frcst.ClassCode
       , cc_type
       , rank
       , keycust1
       , keycust2
       , keycust3
       , sales1
       , sales2
       , sales3
       , SUM(ship2017.GrossSales) AS gross_sales2017
       , SUM(ship2017.QtyShip + ( (ship2017.QtyOrd - ship2017.QtyShip) * 0.25) ) AS frcst_qty2017
       , SUM(ship2018.GrossSales) AS gross_sales2018
       , SUM(ship2018.QtyShip + ( (ship2018.QtyOrd - ship2018.QtyShip) * 0.25) ) AS frcst_qty2018
       , SUM(ship2019.GrossSales) AS gross_sales2019
       , SUM(ship2019.QtyShip + ( (ship2019.QtyOrd - ship2019.QtyShip) * 0.25) ) AS frcst_qty2019
       , SUM(ship2020.GrossSales) AS gross_sales2020
       , SUM(ship2020.QtyShip + ( (ship2020.QtyOrd - ship2017.QtyShip) * 0.25) ) AS frcst_qty2020
  FROM FrcstFactTbl frcst
  JOIN account_hierarchy_lu account
    ON frcst.AccountNumber = account.account_number
  JOIN cc_type_lu cct
    ON frcst.ClassCode = cct.class_code
  JOIN pop_code_lu pop
    ON frcst.PartNumber = pop.PartNumber
  JOIN FrcstFactTbl ship2017
    ON frcst.mnth = ship2017.mnth
   AND frcst.LineCode = ship2017.LineCode
   AND frcst.ClassCode = ship2017.ClassCode
   AND frcst.AccountNumber = ship2017.AccountNumber
  JOIN FrcstFactTbl ship2018
    ON frcst.mnth = ship2018.mnth
   AND frcst.LineCode = ship2018.LineCode
   AND frcst.ClassCode = ship2018.ClassCode
   AND frcst.AccountNumber = ship2018.AccountNumber
  JOIN FrcstFactTbl ship2019
    ON frcst.mnth = ship2019.mnth
   AND frcst.LineCode = ship2019.LineCode
   AND frcst.ClassCode = ship2019.ClassCode
   AND frcst.AccountNumber = ship2019.AccountNumber
  JOIN FrcstFactTbl ship2020
    ON frcst.mnth = ship2020.mnth
   AND frcst.LineCode = ship2020.LineCode
   AND frcst.ClassCode = ship2020.ClassCode
   AND frcst.AccountNumber = ship2019.AccountNumber
 WHERE YEAR(ship2017.InvDate) = '2017'
   AND YEAR(ship2018.InvDate) = '2018'
   AND YEAR(ship2019.InvDate) = '2019'
   AND YEAR(ship2020.InvDate) = '2020'
GROUP BY mnth, frcst.LineCode, frcst.ClassCode, cc_type, rank, keycust1, keycust2, keycust3, sales1, sales2, sales3
ORDER BY mnth 

唯一可以使用列别名(mnth 的别名)的地方是在 order by 子句中。在其他任何地方你都必须使用计算 MONTH(frcst.InvDate) 或者在子查询中计算它。

我会使用子查询,即替换此行:

FROM FrcstFactTbl frcst

有了这个:

FROM (
  select *, MONTH(frcst.InvDate) AS mnth
  from FrcstFactTbl
) frcst

当然还要替换第一行:

SELECT MONTH(frcst.InvDate) AS mnth

SELECT mnth

谢谢大家。下面是最终有效的查询。

SELECT 
        masterlist.Month          AS [Month]
       ,masterlist.LineCode AS [Line Code]
       ,masterlist.ClassCode      AS [Class Code]
       ,ct.cc_type                       AS [Class Code Type]
       ,pop.pop_code              AS [Pop Code]
       ,ah.keycust1               AS [Key Cust1]
       ,ah.keycust2               AS [KeyCust2 - Territory]
       ,ah.keycust3               AS [Key cust3]
       ,ah.sales1                        AS [Sales1]
       ,ah.sales2                        AS [Sales2]
       ,ah.sales3                        AS [Sales3]
       ,SUM(ff2017.[Gross]) AS [2017 Gross]
       ,SUM (ff2017.QtyShip + ( (ff2017.QtyOrd - ff2017.QtyShip) * 0.25) ) AS [2017 Forecast Qty]
       ,SUM(ff2018.[Gross]) AS [2018 Gross]
       ,SUM (ff2018.QtyShip + ( (ff2018.QtyOrd - ff2018.QtyShip) * 0.25) ) AS [2018 Forecast Qty]
       ,SUM(ff2019.[Gross]) AS [2019 Gross]
       ,SUM (ff2019.QtyShip + ( (ff2019.QtyOrd - ff2019.QtyShip) * 0.25) ) AS [2019 Forecast Qty]
       ,SUM(ff2020.[Gross]) AS [2020 Gross]
       ,SUM (ff2020.QtyShip + ( (ff2020.QtyOrd - ff2020.QtyShip) * 0.25) ) AS [2020 Forecast Qty]
FROM (
              SELECT 
                     DISTINCT ff.AccountNumber, MONTH(InvDate) AS Month,LineCode,ClassCode,ff.PartNumber
              FROM FrcstFactTbl ff

       ) AS masterlist
       LEFT OUTER JOIN 
       (
              SELECT 
                      AccountNumber
                     ,PartNumber 
                     ,Month(InvDate)            AS Month
                     ,SUM(GrossSales)     AS [Gross]    
                     ,SUM (QtyShip)             AS [QtyShip]
                     ,SUM(QtyOrd)         AS [QtyOrd]
              FROM FrcstFactTbl WHERE Year(InvDate)=2017
              GROUP BY AccountNumber, PartNumber, Month(InvDate)
       ) AS ff2017 ON masterlist.Month = ff2017.Month AND masterlist.AccountNumber = ff2017.AccountNumber AND masterlist.PartNumber = ff2017.PartNumber
       LEFT OUTER JOIN 
       (
              SELECT 
                      AccountNumber
                     ,PartNumber 
                     ,Month(InvDate)            AS Month
                     ,SUM(GrossSales)     AS [Gross]    
                     ,SUM (QtyShip)             AS [QtyShip]
                     ,SUM(QtyOrd)         AS [QtyOrd]
              FROM FrcstFactTbl WHERE Year(InvDate)=2018
              GROUP BY AccountNumber, PartNumber, Month(InvDate)
       ) AS ff2018 ON masterlist.Month = ff2018.Month AND masterlist.AccountNumber = ff2018.AccountNumber AND masterlist.PartNumber = ff2018.PartNumber
       LEFT OUTER JOIN 
       (
              SELECT 
                      AccountNumber
                     ,PartNumber 
                     ,Month(InvDate)            AS Month
                     ,SUM(GrossSales)     AS [Gross]    
                     ,SUM (QtyShip)             AS [QtyShip]
                     ,SUM(QtyOrd)         AS [QtyOrd]
              FROM FrcstFactTbl WHERE Year(InvDate)=2019
              GROUP BY AccountNumber, PartNumber, Month(InvDate)
       ) AS ff2019 ON masterlist.Month = ff2019.Month AND masterlist.AccountNumber = ff2019.AccountNumber AND masterlist.PartNumber = ff2019.PartNumber
       LEFT OUTER JOIN 
       (
              SELECT 
                      AccountNumber
                     ,PartNumber 
                     ,Month(InvDate)            AS Month
                     ,SUM(GrossSales)     AS [Gross]    
                     ,SUM (QtyShip)             AS [QtyShip]
                     ,SUM(QtyOrd)         AS [QtyOrd]
              FROM FrcstFactTbl WHERE Year(InvDate)=2020
              GROUP BY AccountNumber, PartNumber, Month(InvDate)
       ) AS ff2020 ON masterlist.Month = ff2020.Month AND masterlist.AccountNumber = ff2020.AccountNumber AND masterlist.PartNumber = ff2020.PartNumber
       LEFT OUTER JOIN cc_type_lu ct ON masterlist.ClassCode = ct.class_code
       LEFT OUTER JOIN pop_code_lu pop ON masterlist.PartNumber = pop.PartNumber
       LEFT OUTER JOIN account_hierarchy_lu ah ON masterlist.AccountNumber = ah.account_number
--ORDER BY masterlist.Month
GROUP BY  masterlist.Month        
              ,masterlist.LineCode      
              ,masterlist.ClassCode     
              ,ct.cc_type  
              ,pop.pop_code
              ,ah.keycust1        
              ,ah.keycust2 
              ,ah.keycust3 
              ,ah.sales1          
              ,ah.sales2          
              ,ah.sales3