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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:34:58+00:00 2026-05-26T00:34:58+00:00

I am not sure this is possible with SQL (say we are executing on

  • 0

I am not sure this is possible with SQL (say we are executing on DB2) – infact I would say it is not, but thought I would ask some folk who are more experienced in SQL than I am to comment. I have described the problem and what I would like to happen below. Please let me know if you see a way to do this, if not I guess I am down the path of retrieving the data in batches or something like that. Thanks a lot.

MAIN TABLE

| REF_TABLE   | RECORD_NO      | GROUPID  |
|   TABLE1    |   1            |  BLUE    |
|   TABLE1    |   2            |  BLUE    |
|   TABLE2    |   3            |  GREEN   |
|   TABLE3    |   4            |  BLUE    |
|   TABLE2    |   5            |  GREEN   |
|   TABLE4    |   6            |  BLUE    |

TABLE1

|  RECORD_NO |   NAME    | VALUE     |
|   1        |   NAMEX   |  RANDOM1  |
|   2        |   NAMEY   |  RANDOM2  |

TABLE2

|  RECORD_NO |   NAME    |  VALUE     |
|   3        |   NAMEB   |  RANDOM10  |
|   5        |   NAMEC   |  RANDOM9   |

TABLE3

| RECORD_NO  | NAME      | VALUE      |
|    4       |   NAMET   |  RANDOM77  |

TABLE4

| RECORD_NO  | NAME      | VALUE      |
|   6        |   NAMET   |  RANDOM77  |
|   7        |   NAMEZ   |  RANDOM99  |

So I have some criteria for querying MAIN TABLE e.g.

SELECT REF_TABLE, RECORD_NUMBER WHERE GROUPID = 'BLUE'

But I also want to include the appropiate values from the other tables which are referenced via REF_TABLE. So the result would be:

| REF_TABLE   | RECORD_NUMBER  | NAME    | VALUE     |
|   TABLE1    |   1            | NAMEX   |  RANDOM1  |
|   TABLE1    |   2            | NAMEY   |  RANDOM2  |
|   TABLE3    |   4            | NAMET   |  RANDOM77 |
|   TABLE4    |   6            | NAMET   |  RANDOM77 |

In this case TABLE1, TABLE3 and TABLE4 have their name and value columns merged into a single result set. The table is of course retrieved from the ref_table column of main table. The list of ref tables in finite, so I could hardcode the table names in SQL statements in the query (to avoid dynamically setting the table name) e.g. IF TABLE1 SELECT FROM SCHEMA.TABLE1 (maybe CASE).

Restructuring the tables is not an option and the number of returned results may be 10000s.

Preferable I would want this all as a single query.

Thanks.

  • 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-26T00:34:59+00:00Added an answer on May 26, 2026 at 12:34 am

    One can also take the UNIONs from Hemal Panya’s answer out of the view and into your query…

    SELECT record_no, groupid, name, value
    FROM main_table INNER JOIN ref_table1 on main_table.record_no = ref_table1.record_no
    WHERE main_table.ref_table = 'TABLE1'
    
    UNION ALL
    
    SELECT record_no, groupid, name, value
    FROM main_table INNER JOIN ref_table2 on main_table.record_no = ref_table2.record_no
    WHERE main_table.ref_table = 'TABLE2'
    
    UNION ALL
    
    etc, etc...
    

    This is probably much faster than tryign to use CASE…

    SELECT
      main_table.record_no,
      main_table.groupid,
      CASE main_table.ref_table WHEN 'Table1' THEN Table1.name
                                WHEN 'Table2' THEN Table2.name
                                etc, etc
      END,
      CASE main_table.ref_table WHEN 'Table1' THEN Table1.value
                                WHEN 'Table2' THEN Table2.value
                                etc, etc
      END
    FROM
      main_table
    LEFT JOIN
      Table1
        ON  main_table.record_no = Table1.record_no
        AND main_table.ref_table = 'Table1'
    LEFT JOIN
      Table2
        ON  main_table.record_no = Table2.record_no
        AND main_table.ref_table = 'Table2'
    etc, etc
    

    I would, however, recommend against any of these options. It feels as though your schema is designed against the natural behaviour of SQL. You may either need a new structure, or be better suited to a different environment. Without knowing more details, however, it’s impossible to advise on a natural relational structure that would meet your needs without needing this kind of ‘conditional join’.

    On that basis, I’d be intrigued to know why it is that you are hesitent to use a view to unify your disperate data. It appears, in a vacuum, to be th emost sensible option…

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

Sidebar

Related Questions

Not sure this is possible, but looking to write a script that would return
I'm not sure if this is possible from with a SQL query, but I'll
I'm not sure this is possible, but in ruby, you can dynamically call a
I'm not sure this is possible, but is there a syntax to be used
Not sure if this is possible in one Makefile alone, but I was hoping
I'm not sure if this is possible (complete non-flash developer speaking), but we have
I'm not sure if this is possible but I want to see objects created
This would be hard with pure SQL and I'm REALLY not sure how to
Hi I'm sure this is possible but am not sure how to go about
I'm not even sure this is possible to do efficiently, but here's my problem:

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.