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 8347191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:17:08+00:00 2026-06-09T07:17:08+00:00

I wonder which solution is better. I have to declare some variables in view,

  • 0

I wonder which solution is better. I have to declare some variables in view, which are calculated using T-SQL date functions (DATEADD,DATEPART,GETDATE(),etc).

After some research I’ve write this:

WITH DatePeriods(ThisWeek,LastWeek,MonthToDate,QuarterToDate,YearToDate) AS
(
    SELECT  "...date functions..." AS ThisWeek
            "...date functions..." AS LastWeek
            "...date functions..." AS MonthToDate
            "...date functions..." AS QuarterToDate
            "...date functions..." AS YearToDate
)
SELECT Desciption,Value
FROM DatePeriods
UNPIVOT
(
    Value FOR Desciption IN (ThisWeek,LastWeek,MonthToDate,QuarterToDate,YearToDate)
) AS Source

And it looks cool because I have “cte” and “unpivot”. If I want to add other date variable I only should insert in the CTE’s select.

And the other solution is using ordinary “union”:

SELECT  'ThisWeek',"...date functions..." AS ThisWeek
UNION
SELECT  'LastWeek',"...date functions..." AS LastWeek
UNION
SELECT  'MonthToDate',"...date functions..." AS MonthToDate
UNION
SELECT  'QuarterToDate',"...date functions..." AS QuarterToDate
UNION
SELECT  'YearToDate',"...date functions..." AS YearToDate

I think it is not so good because a new date variable means new union, but after all union between few variables only.

Can anyone tell me which technique is the good practice in this case or even offers other solution?

Thanks in advance.

EDIT:

This is the output that I want:

Desciption      Value
ThisWeek        2012-08-05 08:55:23.013
LastWeek        2012-07-29 08:55:23.013
MonthToDate     2012-07-08 08:55:23.013
QuarterToDate   2012-05-08 08:55:23.013
YearToDate      2011-08-08 08:55:23.013
  • 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-09T07:17:09+00:00Added an answer on June 9, 2026 at 7:17 am

    If you have a look at the query plans you will see that your union version has much higher cost than your unpivot version. But if you change to union all instead it will be better than unpivot.

    If you are on SQL Server 2008 or later you can use values instead and according to the execution plan the cost is the same as union all.

    Values version:

    select Description, Value
    from (values ('ThisWeek',      getdate()+1),
                 ('LastWeek',      getdate()+2),
                 ('MonthToDate',   getdate()+3),
                 ('QuarterToDate', getdate()+4),
                 ('YearToDate',    getdate()+5)
         ) as T(Description, Value)
    

    Union all version:

    SELECT  'ThisWeek' AS Description, getdate()+1 AS Value
    UNION ALL
    SELECT  'LastWeek', getdate()+2
    UNION ALL
    SELECT  'MonthToDate', getdate()+3 
    UNION ALL
    SELECT  'QuarterToDate', getdate()+4 
    UNION ALL
    SELECT  'YearToDate', getdate()+5
    

    The reason for union is slower than union all is because it tries to remove duplicates from the result set where union all incorporates all rows regardless of value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is a simple exercice, probably some solution better than others, but I wonder which
I've been using debug() more often now, but sometimes I wonder which functions have
I have a process/Linux, which runs out of memory very soon, and I wonder
I currently encounter a problem and wonder if there exists a better solution for
I am new to Lucene and I wonder, which directory is the best solution
I use Entity Framework to access my SQL data. I have some constraints in
I wonder, if a solution exists, which statically analyzes PHP source-files. I'm aware of
I wonder which method among the following three methods is the best to perform
Having a look at BDD frameworks and I can't help but wonder which one
1 -I wonder that how to get which control posted back the page and

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.