Skip to Main Content
  • Questions
  • selecting table column based on lookup table

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Subhash.

Asked: November 09, 2017 - 5:04 pm UTC

Last updated: November 11, 2017 - 8:33 am UTC

Version: 12.1

Viewed 1000+ times

You Asked

Hi

I am trying to get columns from a table only if that column value is set as "YES" in another lookup table.
Please help me to get the query for the same.

I have a lookup table like this:

create table cust_bug_lookup(Title varchar2(100), Last_update DATE, created_by varchar2(100),START_DATE DATE, END_DATE DATE,product_id varchar2(100),BUG_NUMBER varchar2(100),component varchar2(100),analyzed_by varchar2(100),root_cause varchar2(100));
insert into cust_bug_lookup(Title,product_id,BUG_NUMBER, component, analyzed_by, root_cause ) values('test123',5,'yes','no','yes','no');

I have another table:

create table cust_bug_value(product_id number, bug_number number, component varchar2(50), analyzed_by varchar2(50), root_cause varchar2(50));
insert into cust_bug_value(product_id, bug_number, component, analyzed_by, root_cause) values(5,1234564,'extract','subhash','this is test issue')



Now I want to select only columns from cust_bug_value where column value is set as "yes" in cust_bug_lookup

so my select query should return output like:
product_id bug_number analyzed_by
---------- ----------- ----------
5 1234564 subhash


my select query should be like as follows:
select ---------------- from cust_bug_value where product_id=5;


could you please let me know the select query for above output using above 2 tables?

Thanks,
Subhash


and Connor said...

I think you're heading down a bad road here.

There is a line to be drawn between

- what the database holds (ie, data)
- what is presented to the user/customer (ie, the window on that data)

So there is nothing wrong with holding the *information* about the output in the database (ie, your cust_bug_lookup table), but when it comes to querying the data, I think you'll be much better off with your query to the database getting (in general) all the columns from cust_bug_value, and your application tool (eg a reporting tool, or application express, etc) being in charge of using the information in cust_bug_lookup to then render that information to the screen.

Otherwise you are definitely into dynamic SQL territory, which will just be unpleasant all around, because *every* query will need to be dynamically generated.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Pls have a look again

Subhash Kumar, November 10, 2017 - 5:21 am UTC

Thanks Connor for your response.

Actually lookup table is updated my user only and cust_bug_value column will be based on the user entries given in lookup table. This is basically for creating a report for user in apex which will have column specified in lookup table.

What would be alternate solution of this if query is not feasible like this.

Thanks,
Subhash
Connor McDonald
November 11, 2017 - 8:33 am UTC

As I said, defer the columns to the rendering tool.

So for Apex, your report *query* can get *all* of the columns, and then use Apex to selectively show/hide columns based on your metadata table.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library