Oracle query assistance

CalmSoul
CalmSoul used Ask the Experts™
on
I am looking for oracle query to return column name only if there is data in the column cell  and also need to do where on column with certain value. For example

(table 1)

col1 col2 col3
1      (null)  x
2        3      y

OUTPUT will be col1 because its not null and col3=x

i.e. return column name of all column which are "NOT NULL" and where col3=x

Please assist.... I hope I explained it correctly
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
SELECT 'col1'
FROM table1
WHERE col3 = 'x'
AND  (SELECT Count(*) FROM table1 WHERE col1 IS NULL) = 0
UNION
SELECT 'col2'
FROM table1
WHERE col3 = 'x'
AND (SELECT Count(*) FROM table1 WHERE col2 IS NULL) = 0

Author

Commented:
Hi Fomand: I have more than 30 columns will this work?
Yes, it will. You need to add

UNION
SELECT 'col2'
FROM table1
WHERE col3 = 'x'
AND (SELECT Count(*) FROM table1 WHERE col2 IS NULL) = 0

for every column.

You can generate the query dynamically, but it is quite complex.
To select column list from systables requires some privileges.
Create the list on your own and process it - it is almost the same as to add and to edit UNION...
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
I'm not sure you need to UNION all 30 columns to obtain what you want but I also do not fully understand your requirement.

>>return column name of all column which are "NOT NULL" and where col3=x

So if you add two additional rows to the table what is the expected output:
col1 col2 col3
1      (null)  x
2        3      y
(null)        4       x
5         6          x
Walter RitzelSenior Software Engineer

Commented:
I agree with slightwv, the requirement is not clear enough.

Assuming the the solution from formand do what you need, there is room for a little improvement:

each select could be written as below:

SELECT 'col1'
FROM table1
WHERE col3 = 'x'
AND col1 is not null 

Open in new window


Regards,
Walter.

Author

Commented:
here is sample data: https://docs.google.com/spreadsheet/ccc?key=0As8ubhOj3mzLdDVMMlhJYzE2WjBFa3BzSk1aWmdkeXc&usp=sharing

- need to find all columns where data is present ... group by column F
Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
>>here is sample data

Any reason you didn't upload it to experts-exchange and this question?

>>need to find all columns where data is present ... group by column F

Requirement still not clear.

Is that data supposed to be a table with 7 columns containing 4 sample rows of data?

Given that data, what should the results look like?

Author

Commented:
I can't share the real data and its private information this is just a sample.. Uploading EE is not working ...

I don't how to put the requirements but in plain english

I need column names of all columns where data is present - for each unique entry in column F in the sample.

Using following query I am getting results but I want to optimize it

select max(col1), max(col2) ... group by col6
Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
>>I can't share the real data and its private information this is just a sample

I don't need real data.  I just need to understand the requirement and I can apply that to the sample data.

You didn't answer my question:
Is that data supposed to be a table with 7 columns containing 4 sample rows of data?

>>I don't how to put the requirements but in plain english

That is my problem.  I'm not understanding from the typed description.

Try not explaining it.  Show me the expected output.  If I cannot deduce the requirement from the inputs and outputs, I'll ask for clarification.

>>select max(col1), max(col2) ... group by col6

See, this is nothing like what I was understanding from any previous post.

If you want the MAX value for ALL columns except one or two, I think that is about as optimized as you can get.

If you get full table scans and very few columns have data then indexes on those columns might help but you will need to try it.  Indexes that fix one query can hurt others.

Author

Commented:
You didn't answer my question:
Is that data supposed to be a table with 7 columns containing 4 sample rows of data?

Real data has 30 columns in total... and 1000 records

Sample Output from the sample data provided above:

Coffee - E
Tea - B,C,D,E
Green Tea - A,D
Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
Thanks.  I understand the requirement as far as output.

how do you want those results?

Three rows with two columns and a CSV for the hits:
category       not null columns
-----------    -------------------------
Coffee         E
Tea            B,C,D,E
Green Tea      A,D

Open in new window


Or something else?

Author

Commented:
Correct - Just like you have showed .. thats perfect
Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
Sorry but two more questions:

What is your Oracle version (all 4 numbers please: 11.2.0.4)?
What is the maximum length of the resulting CSV string?  Can it EVER exceed 4000 characters?

Author

Commented:
What is your Oracle version (all 4 numbers please: 11.2.0.4)?
I have oracle 8 - I am using toad data point - its connects to the database but via warning using 10g drivers. I don't how to get complete version via toad ... do you know?

What is the maximum length of the resulting CSV string?  Can it EVER exceed 4000 characters?
No it will not increase 4000 in lenght
Most Valuable Expert 2012
Distinguished Expert 2018
Commented:
>>I have oracle 8

OK, what are the other 3 numbers?  8.0.0.1, 8.2.0.4, what?

>>I don't how to get complete version via toad

Toad version doesn't matter.  Just the database  version.

>>Can it EVER exceed 4000 characters?  No it will increase 4000 in lenght

I do not understand this.  Can the resulting string be greater than 4000 characters once concatenated together?  If so, we might have some problems.


While I wait:
Here is a complete test case that provides the expected results using your provided sample data.

If it doesn't work, please add rows/columns to the tables and update your expected results.

It should run on Oracle 8 no matter the version (I forget when the CASE statement came out) but I don't have anything near that old still around to confirm it.

If the case statement doesn't run, we can switch to the older DECODE.

drop table tab1 purge;

create table tab1(
Store1 char(1), Store2 char(1), Store3 char(1), Store4 char(1), Store5 char(1), mytag varchar2(10)
);


insert into tab1 values(null,null,null,null,'1','Coffee');
insert into tab1 values(null,'1','1','1','1','Tea');
insert into tab1 values('1',null,null,'1',null,'Green Tea');
commit;

select mytag, 
	rtrim(
		case when store1 is not null then 'A,' end || 
		case when store2 is not null then 'B,' end || 
		case when store3 is not null then 'C,' end || 
		case when store4 is not null then 'D,' end || 
		case when store5 is not null then 'E' end 
	,',')
	mycsv
from tab1
/

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial