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

  • Home
  • SEARCH
  • 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 8398063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:56:31+00:00 2026-06-09T20:56:31+00:00

I have a question about sub-queries and case statements I have two case statements

  • 0

I have a question about sub-queries and case statements

I have two case statements in the same query:

  • One has a sub-query that is used to determine if a column has a match.
  • I’d like the other to check if theres a match [among other checks], then tag a value.
  • However, t-SQL will not let me reference my first field (generated from the case statement) within my second case statement.
  • This forces me to add the subquery into my second case statement and do away with the first case statement
  • When I do this, my query goes from 13 seconds to 2.5 minutes
  • When I remove the subquery altogether from my query, it takes 8 seconds to run

Question 1: Can case-statement-generated fields be referenced in subsequent case statements in the same query?

Question 2: Why does my query take only 5 seconds longer when I have the subquery in an isolated case statement but 2 minutes longer when that subquery is in a case statement that has 4-5 other checks?

1st Case Statement

CASE WHEN (SELECT xxx.xxx from xxx) THEN 'Y' 
     END AS "Match_Ind",

Second Case Statement

CASE WHEN condition 1 = true THEN 'cond1'
    WHEN condition 2 = true THEN 'cond2'
    WHEN Match_Ind = 'Y' THEN 'matched'
    END AS "Match Detail"
  • 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-09T20:56:33+00:00Added an answer on June 9, 2026 at 8:56 pm

    You should consider posting your full query but if you want to reference the result of the first CASE inside of another CASE statement, then you can wrap it in a SELECT similar to this:

    select 
        CASE 
            WHEN condition 1 = true THEN 'cond1'
            WHEN condition 2 = true THEN 'cond2'
            WHEN Match_Ind = 'Y' THEN 'matched'
        END AS Match Detail
    from 
    (
        SELECT CASE 
            WHEN conditionHere -- (SELECT xxx.xxx from xxx)
            THEN 'Y' 
            END AS Match_Ind,
            othercols
        from yourtable
    ) x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question comes after a friend's comment. He said that when a query has
I have a question about inner-workings of Python sub-interpreter initialization (from Python/C API) and
I have a quick question that hopefully someone has worked through before. In the
Following on from a previous question about sub-selects, I have an SQL statement with
I have a question about using subqueries in an Update statement. My example: UPDATE
I have question about parsing in Html helper : I have sth like: @foreach
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have question about XSLT1.0. The task is to write out in HTML all
I have question about normalization. Suppose I have an applications dealing with songs. First
I have question about interpreting strings as packed binary data in C++. In python,

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.