简体   繁体   中英

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

i have the following query :

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

My #C-table is declared as follows:

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
)

Now when I run the query, I get the message : Invalid column name 'CalanderDate'.

when I remove the UNION it works fine. But I do need the union between the different tables.

Can anyone help me?

I think I've found the problem:

As I suspected, it's caused by your mixture of join styles:

This on clause is invalid on FREQ.Task = INC.Task AND FREQ.Contract = INC.Contract AND CalanderDate = INC.[EstimatedCompletionStamp] since you are using an implicit join on calander .

I think that this should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM