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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:20:51+00:00 2026-05-22T20:20:51+00:00

I would like to know if the following is possible in SQL server 2005.

  • 0

I would like to know if the following is possible in SQL server 2005. Column A and B are calculated using other case statements in my actual stored proc. I don’t want to repeat the same for another field unnecessarily. If this is not syntactically possible, any other ideas?

SELECT A, B, CASE WHEN column1='1' THEN A ELSE B END Col1.

Modified version of actual query provided as requested. CTE kind of seems to be tough in this model. WANNABE is the column I want to accomplish in the sub select statement.

SELECT 1 AS Region, 'Test', 
        CAST(Work AS NUMERIC(18,2)) Work,
        Work + 2 AS Work2,
        WANNABE

        FROM
        (
            SELECT 
                ROW_NUMBER() OVER(PARTITION BY G.Value, C.C, FR.Mod1 ORDER BY FR.Date DESC, FG.Date DESC, FC.Date DESC) ROW,
                CASE WHEN COALESCE(FR.Mod1, '') = '' THEN '' ELSE FR.Mod1 END Mod1,

                CASE WHEN @var1=1 AND @var2 = 1 THEN FR.Col1 *  G.Value 
                     WHEN @var1=1 AND @var2 = 0 THEN FP.Col1 *  G.Value END Work,

                CASE WHEN 1=1 THEN Work ELSE 1 END WANNABE,

                (
                    SELECT Col3
                    FROM Table2 
                    WHERE c = FR.Value
                ) AS Custom

            FROM MainTable FR
            JOIN @C C ON FR.Col2 = C.Col2
            LEFT JOIN Function1(@VersionDate) cv ON cv.Code = C.Code    
            LEFT JOIN Function2(@VersionDate) hv ON hv.Code = C.Code    
            LEFT JOIN @G G ON 1 = 1
            LEFT JOIN SubTable1 FG ON FG.Number = G.Value, 2 AND FG.Date = @VersionDate
            LEFT JOIN SubTable2 FO ON FO.Number = G.Value
                AND FO.Date = @VersionDate AND FO.Code = FR.Code AND FR.Mod1 = FO.Mod1
            LEFT JOIN SubTable3 FP ON FP.Code = FR.Code AND FP.VersionDate = @Versiondate
             AND CASE WHEN DATALENGTH(FR.Mod1) = 0 THEN '00' ELSE FR.Mod1 END = CASE WHEN DATALENGTH(FP.Mod1) = 0 THEN '00' ELSE FP.Mod1 END
            LEFT JOIN SubTable4 FC ON FC.Date = @VersionDate
            WHERE  FR.Date = @VersionDate 
        ) x
        WHERE x.Row = 1
        AND RTRIM(LTRIM(x.Col1)) IN ('', '2')
  • 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-05-22T20:20:51+00:00Added an answer on May 22, 2026 at 8:20 pm

    You can define the A,B column aliases in a CTE then reference them in an outer select.

    ;WITH CTE AS
    (
    SELECT CASE ... END AS A, 
           CASE ... END  AS B, 
           column1
    FROM your_table
    )
    SELECT A, 
           B, 
           CASE WHEN column1='1' THEN A ELSE B END Col1
    FROM  CTE 
    

    Similarly you can also define them in a CROSS APPLY that is sometimes a bit less verbose.
    A silly example just to show the syntax is

    SELECT A,
           B,
           CASE WHEN type='P' THEN A ELSE B END Col1 
    FROM master..spt_values
    CROSS APPLY (SELECT CASE WHEN number %2 = 1 THEN 1 END, 
                        CASE WHEN number %2 = 0 THEN 0 END) T(A,B)
    

    Following your update you can replace the derived table with a CTE and nest CTEs as follows

    ;WITH x as
    (
                    SELECT 
                        ROW_NUMBER() OVER(PARTITION BY G.Value, C.Code, FR.Mod1 ORDER BY FR.Date DESC, FG.Date DESC, FC.Date DESC) ROW,
    ...<snip>                
                    WHERE  FR.Date = @VersionDate 
    ),
    x2 As
    (
    SELECT *,
           CASE WHEN 1=1 THEN Work ELSE 1 END WANNABE
    FROM x
    )
        SELECT 1 AS Region, 'Test', 
                CAST(Work AS NUMERIC(18,2)) Work,
                Work + 2 AS Work2,
                WANNABE
                FROM x2
                WHERE x2.Row = 1
                AND RTRIM(LTRIM(x2.Col1)) IN ('', '2')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if it is possible to simplify the following process:
I would like to know whether the following are possible in the current windows
I would like to know if the following is possible in git: Suppose I
I would like to know if you find the following pattern meaningful in domain
I would like to know if I can open 2 different diagrams using MS
Does anybody know if it's possible to do a %LIKE% search against a column
I would like to know where put monkey patching code like the following in
I would like to know whether it is possible to run a C# console
I have the following three SQL queries. I would like to combine them into
I would like to know which dependency described in my pom.xml brings a transitive

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.