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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:37:18+00:00 2026-05-16T16:37:18+00:00

I am trying to write a SQL query where I am joining two tables

  • 0

I am trying to write a SQL query where I am joining two tables and retrieving a few columns. Once this is done, based on a two fields (source table enum, id in the corresponding source table), I need to retrieve a value matching the id in the source table.

The issue is that I cannot join all the source tables, and want to do something like this:

Select 
  X.Col1,
  X.Col2,
  Y.Col1,
  Y.Col2,
  CASE
    WHEN Y.TableID = 4 THEN Select t4.Col1 FROM TableFour t4 WHERE t4.id = Y.FileID
    WHEN Y.TableID = 5 THEN Select t5.Col4 FROM TableFive t5 WHERE t5.id = Y.FileID
  END
FROM Table X INNER JOIN Table Y ON X.ID = Y.XID

I can guarantee the value being retrieved from all the source tables will be the same (i.e. nvarchar).

Nesting SQL queries inside the CASE statement doesn’t seem to work, I am throwing it you guys. Any ideas with this problem?

Hope I explained the question adequately. If you are unsure, make a comment so I can clarify it.

Cheers in advance!

  • 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-16T16:37:19+00:00Added an answer on May 16, 2026 at 4:37 pm

    I don’t understand what do you mean by “cannot join all the source tables”, can you elaborate?
    I would use outer joins:

    Select 
      X.Col1,
      X.Col2,
      Y.Col1,
      Y.Col2,
      COALESCE(t4.Col1 ,t5.Col4)
      END
    FROM Table X INNER JOIN Table Y ON X.ID = Y.XID
    LEFT OUTER JOIN TableFour t4 ON t4.id = Y.FileID AND Y.TableID = 4
    LEFT OUTER JOIN TableFive t5 ON t5.id = Y.FileID AND Y.TableID = 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a SQL or ActiveRecord query to answer this question: Of
I am trying to write a SQL query that pulls from 3 tables and
This is the SQL query I am trying to write with LINQ: SELECT pd.Description,
I'm trying to write a sql query that shows how often two teams have
I'm trying to write a sql query to the following (from this site http://sqlzoo.net/1b.htm
I am trying to write a SQL query to fetch rows whose fields contain
I am trying write a SQL query that filters a gridview by the fields
I am trying to write an SQL query that will sort based on a
I am trying to write a Microsoft SQL Server query for retrieving the oldest
I am trying to write a SQL query that selects multiple columns from a

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.