T-SQL - except - add date column

  • SELECT 
           [A]
          --,[Date]
          ,
          ,[C]
          ,[D]
          ,[E]
          
      FROM [Database].[dbo].[Table]
      where B < 1000  
      AND   (CheckDate >= getdate()-4 AND 
        CheckDate < getdate())


    EXCEPT


     


    SELECT 
          [A]
          --,[Date]
          ,
          ,[C]
          ,[D]
          ,[E]
          
      FROM [Database].[dbo].[Table]
      where B < 1000  
      AND   (Date >= getdate()-6 AND 
        Date < getdate()-1)


    Every week the date column will have the current date.
    How do I add back the Date column into the above result set.
  • Uncomment it? (remove the double hyphens before ,[Date])

     

    Does that code actually work with the extra comma?

  • Add the date column after the except is excecuted along with the rest of the columns - A,C,D,E

  • For someone with as much experience here as you seem to have, where's the explanation of what you're trying to do? You know, in plain English.

  • For someone with as much experience here as you seem to have, where's the explanation of what you're trying to do? You know, in plain English.

  • I am trying to include the datetime column back into the result set after doing the except.

  • Try something along these lines ...

    WITH cteDiff (
    SELECT A, C, D, E
    FROM [Database].dbo.[Table]
    WHERE B < 1000
    AND ( CheckDate >= GETDATE() -4
    AND CheckDate < GETDATE())
    EXCEPT
    SELECT A, C, D, E
    FROM [Database].dbo.[Table]
    WHERE B < 1000
    AND ( Date >= GETDATE() -6
    AND Date < GETDATE() -1 )
    )
    SELECT src.A, src.[Date], src.C, src.D, src.E
    FROM cteDiff AS xcpt
    JOIN [Database].dbo.[Table] AS src
    ON xcpt.A = src.A
    AND xcpt.C = src.C
    AND xcpt.D = src.D
    AND xcpt.E = src.E
    WHERE src.B < 1000
    AND ( src.CheckDate >= GETDATE() -4
    AND src.CheckDate < GETDATE())

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply