列名称无效 'CalanderDate'。使用 with 和 union 时

Invalid column name 'CalanderDate'. when using a with and a union

我有以下查询:

WITH Calender AS 
(
    SELECT @FromDate AS CalanderDate
    UNION ALL
    SELECT CalanderDate + 1 FROM Calender
    WHERE CalanderDate + 1 <= @ToDate
)
INSERT INTO #C
SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25)
     , case [dbo].[CalculateNumericWeekCode] (4, CalanderDate)
         when 1 then 'A'
         when 2 then 'B'
         when 3 then 'C'
         when 4 then 'D'
         end
        , TT.Name
    , 'Green'
    , 'Green'
    , FREQ.pageid
    ,''
FROM Calender,
     TransportType AS TT INNER JOIN #F AS FREQ ON FREQ.TransportTypeID = TT.TransportTypeId
     WHERE TT.TransportTypeID = @TransportTypeID
UNION
SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25)
     , case [dbo].[CalculateNumericWeekCode] (4, CalanderDate)
         when 1 then 'A'
         when 2 then 'B'
         when 3 then 'C'
         when 4 then 'D'
         end
     , TT.Name
    , Case INC.incidentstatusid
       when 6 -- Approved
          then 'Blue'
       else
          ''
      END
    , 'Green'
    , FREQ.pageid
    ,''
FROM Calender,
     TransportType AS TT INNER JOIN #F AS FREQ ON FREQ.TransportTypeID = TT.TransportTypeId
     INNER JOIN [Incident] AS INC on FREQ.Task = INC.Task AND FREQ.Contract = INC.Contract AND CalanderDate = INC.[EstimatedCompletionStamp]
     INNER JOIN [IncidentStatus] AS INCSTAT ON INCSTAT.IncidentStatusID = INC.IncidentStatusID
     INNER JOIN [IncidentSubType] AS INCSTYP ON INCSTYP.incidentsubtypeid = INC.IncidentSubTypeID and INC.IncidentSubTypeID in (20,21,22,23) and INC.TransportTypeId = TT.TransportTypeID

我的#C-table声明如下:

CREATE TABLE #C
(  
  DateOfTransport DateTime not null,
  WeekCode varchar(1) NOT NULL,
  [TransportType] varchar(25) NULL,
  [CSDColor] varchar(10) not null,
  [WWWColor] varchar(10) not null,
  [PageID] int not null,
  [ToolTip] varchar(250) null
)

现在,当我 运行 查询时,我收到消息:列名无效 'CalanderDate'。

当我删除 UNION 时它工作正常。 但我确实需要不同 table 之间的联合。

谁能帮帮我?

我想我找到了问题所在:

正如我所怀疑的那样,这是由于您混合了连接样式造成的:

on 子句无效 on FREQ.Task = INC.Task AND FREQ.Contract = INC.Contract AND CalanderDate = INC.[EstimatedCompletionStamp],因为您在 calander 上使用隐式联接。

我认为这应该可行:

SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25)
     , case [dbo].[CalculateNumericWeekCode] (4, CalanderDate)
         when 1 then 'A'
         when 2 then 'B'
         when 3 then 'C'
         when 4 then 'D'
         end
     , TT.Name
    , Case INC.incidentstatusid
       when 6 -- Approved
          then 'Blue'
       else
          ''
      END
    , 'Green'
    , FREQ.pageid
    ,''
FROM TransportType AS TT 
     INNER JOIN #F AS FREQ ON FREQ.TransportTypeID = TT.TransportTypeId     
     INNER JOIN [Incident] AS INC on FREQ.Task = INC.Task AND FREQ.Contract = INC.Contract 
     INNER JOIN Calender ON CalanderDate = INC.[EstimatedCompletionStamp]
     INNER JOIN [IncidentStatus] AS INCSTAT ON INCSTAT.IncidentStatusID = INC.IncidentStatusID
     INNER JOIN [IncidentSubType] AS INCSTYP ON INCSTYP.incidentsubtypeid = INC.IncidentSubTypeID and INC.IncidentSubTypeID in (20,21,22,23) and INC.TransportTypeId = TT.TransportTypeID