Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8294285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:08:55+00:00 2026-06-08T14:08:55+00:00

I need to create a view – it is composed by five UNION ALL

  • 0

I need to create a view – it is composed by five UNION ALL statements. The difference between each statement is that the data is filter by different period:

For example:

SELECT  RecordName
       ,CASE
        WHEN RecordDate > DATEADD(WEEK,-1,GETUTCDATE()) THEN 'This week'
        END
UNION ALL
SELECT  RecordName
       ,CASE
        WHEN RecordDate > DATEADD(WEEK,-2,GETUTCDATE()) THEN 'Previos week'
        END
SELECT  RecordName
       ,CASE
        WHEN RecordDate > DATEADD(Year,-1,GETUTCDATE()) THEN 'Year ago'
        END

Then I am creating a pivot using the view.

Anyway, the “date” condition is calculate in more complex way. I am using GETUTCDATE() function too and this will return different value any millisecond.

That’s why I want to use a CTE expression in order to initialize all date condition variables or to do the calculations one time only in the CTE, and then to use this date conditions in the SELECT-UNION clause.

The problem is, I am not able to join the information from the CTE with the SELECT statement below and when I try to use the date conditions directly (without join) it is not working (“Error – Invalid Column name”).

This is a example of what I am trying to do:

WITH DatePeriods(ThisWeek,LastWeek,MonthToDate,QuarterToDate,YearToDate) AS
(
    SELECT  DATEADD(WEEK,-1,GETUTCDATE())  AS ThisWeek
           ,...                            AS LastWeek
           ,...                            AS MonthToDate
           ,...                            AS QuarterToDate
           ,DATEADD(YEAR,-1,GETUTCDATE())  AS YearToDate
)

SELECT  RecordName
       ,CASE
        WHEN RecordDate > ThisWeek THEN 'This week'
        END
UNION ALL
SELECT  RecordName
       ,CASE
        WHEN RecordDate > LastWeek THEN 'Previos week'
        END
SELECT  RecordName
       ,CASE
        WHEN RecordDate >YearToDate THEN 'Year ago'
        END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:08:59+00:00Added an answer on June 8, 2026 at 2:08 pm

    You have to use the CTE in the from clause of each query. You can do it with a cross apply.

    WITH DatePeriods(ThisWeek,LastWeek,MonthToDate,QuarterToDate,YearToDate) AS
    (
        SELECT  DATEADD(WEEK,-1,GETUTCDATE())  AS ThisWeek
               ,...                            AS LastWeek
               ,...                            AS MonthToDate
               ,...                            AS QuarterToDate
               ,DATEADD(YEAR,-1,GETUTCDATE())  AS YearToDate
    )
    
    SELECT  RecordName
           ,CASE
            WHEN RecordDate > ThisWeek THEN 'This week'
            END
    FROM YourTable 
      CROSS APPLY DatePeriods 
    UNION ALL
    SELECT  RecordName
           ,CASE
            WHEN RecordDate > LastWeek THEN 'Previos week'
            END
    FROM YourTable 
      CROSS APPLY DatePeriods 
    SELECT  RecordName
           ,CASE
            WHEN RecordDate >YearToDate THEN 'Year ago'
            END
    FROM YourTable 
      CROSS APPLY DatePeriods 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a View that represents an x/y axis. I will read
I understand that we need to create MXML file to define a view. Suppose
UPDATED: I am using MySQL statement to create a view: I need to show
I need to create a view on the iPhone that scrolls horizontally through a
I need to create a view that automatically adds virtual row number in the
I need to create a view or a table valued function that returns one
I need to create a view that show employees that have their Service anniversary
I need to create a view, however the data is generated from an application
I need to create a view called phonelist which list each player, their guardians,
I need to create a view that renders text in columns with a first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.