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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:46:28+00:00 2026-05-26T13:46:28+00:00

I have the following setup: TableA: id=1, type=Air id=2, type=Sea id=3, type=Land … This

  • 0

I have the following setup:

TableA:
 id=1, type=Air
 id=2, type=Sea
 id=3, type=Land
 ... This is a dynamic list, so more could be added

TableB:
 id=42, tableA_Id=1, name=Falcon
 id=43, tableA_Id=1, name=Pigeon
 id=44, tableA_Id=2, name=Shark
 id=45, tableA_Id=3, name=Bear
 id=47, tableA_Id=3, name=Wolf
 ... This is a dynamic list, so more could be added

Desired Output:

SomeOtherTableHeader, Air, Sea, Land, SomeOtherTableHeader
----------------------------------------------------------
someOtherValue, Falcon, Shark, Bear, someOtherValue
someOtherValue, Falcon, Shark, Wolf, someOtherValue
someOtherValue, Pigeon, Shark, Bear, someOtherValue
someOtherValue, Pigeon, Shark, Wolf, someOtherValue
  • Note that the TableA contents are now column headers
  • Note that this query needs to generate a row for every combination of TableB values.

In other words, I am trying to read a list of TableA values and use them to represent column headers. I am trying to create rows based on the combination of values in TableB

Is something like this even possible to do in a query (without a stored procedure)? If so, what would be the best/easiest way to achieve something like this?

Thanks!

  • 1 1 Answer
  • 1 View
  • 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-26T13:46:29+00:00Added an answer on May 26, 2026 at 1:46 pm

    If the contents of tableA are dynamic, you cannot do this with a simple SQL query (since a query needs to have a static set of columns).

    The usual approach is to write a stored procedure that generates your SQL statement on-the-fly; for simplicity, I’ve just used a PL/SQL block:

      -- setup tables + contents
    
    create table tableA(id number, type varchar2(100));
    create table tableB(id number, tableA_id number, name varchar2(100));
    insert into tableA values(1, 'Air');
    insert into tableA values(2, 'Sea');
    insert into tableA values(3, 'Land');
    insert into tableB values(42, 1, 'Falcon');
    insert into tableB values(43, 1, 'Pigeon');
    insert into tableB values(44, 2, 'Shark');
    insert into tableB values(45, 3, 'Bear');
    insert into tableB values(47, 3, 'Wolf');
    commit;
    
    -- build the query (we just print it, usually you'd use something like "open cursor for l_sql"
    declare
      l_sql varchar2(4000);
      l_select  varchar2(4000);
      l_with varchar2(4000);
      l_subviewname varchar2(30);
      l_from varchar2(4000);
    begin
      l_sql := ' with ';
      l_select := ' select ';
      l_from := ' from ';
    
      for cur in (select * from tableA order by id)
      loop 
        l_subviewname := 'v_' || cur.type;
        l_with := l_with || l_subviewname || ' as (select b.* from tableB b, tableA a where b.tableA_id = a.id and a.type = ''' || cur.type || '''),' ||  chr(10);
        l_select := l_select || cur.type || '.name as ' || cur.type || ',';
        l_from := l_from || l_subviewname || ' ' || cur.type || ',';
      end loop;
      -- get rid of trailing ,
      l_with := regexp_replace(l_with, ',\s*$', '');
      l_select := regexp_replace(l_select, ',\s*$', '');
      l_from := regexp_replace(l_from, ',\s*$', '');
    
      l_sql := l_sql || l_with || l_select || l_from;
      dbms_output.put_line(l_sql);
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this following setup, a textarea named with some data in it that
I have the following setup in MS IE8: <table class=my-info> <tbody> <tr> <td class=info-left>First
I have following setup, but when I put 1024 and replace all 512 with
I have following models setup in my Django application class School(models.Model): name = models.TextField()
I have the following setup, and I need to know how to persist state.
I have the following setup, and the empty view text doesn't show up... protected
I have the following setup: eclipse a standard Java project (A) an eclipse plugin
I have the following setup: Web.config has customErrors mode=Off Global.Application_Error() event calls a custom
I have the following setup in Oracle, i.e.: (‘aXYZApple-Au’, ‘aXYZOrange-Au’,’aXYZApple-Readonly’,’aXYZOrange-Write’, ’aXYZApple-Write’, ’aXYZOrange-Read’) What I
I have the following setup. The folder itext is in the top level in

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.