Solved

oracle query help

Posted on 2014-03-04
10
226 Views
Last Modified: 2014-03-11
I am looking for Oracle query with following requirements

- Find all columns where data is present

col1	        col2	        col3	       col4	      col5	      col6

								aa		x
aw		dsa		ds						y
						as				z	

Open in new window


OUTPUT

x col5
y col1,col2,col3
z col4

Open in new window

0
Comment
Question by:CalmSoul
  • 4
  • 4
  • 2
10 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39905543
select col6, case when col1 <> '' then 'col1, ' else '' end ||
                    case when col2 <> '' then 'col2, ' else '' end ||
                    case when col3 <> '' then 'col3, ' else '' end ||
                    case when col4 <> '' then 'col4, ' else '' end ||
                    case when col5 <> '' then 'col5, ' else '' end  "Cols"
from <table>
where
col1 <> '' or
col2 <> '' or
col3 <> '' or
col4 <> '' or
col5 <> ''

note you may need to wrap the columns in isnull  if they are nulls and not blanks.
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 39905566
My data has nulls and blanks? where the wapper should go?
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39906220
Just use the syntax above which is pretty much the same thing I provided in your previous question:
http://www.experts-exchange.com/Database/Oracle/Q_28375718.html

No need for a wrapper.  Just remove the where clause above.

Just like the code above and taken from your previous question (setup for tables is still in there):
select mytag, 
	rtrim(
		case when store1 is not null then 'store1,' end || 
		case when store2 is not null then 'store2,' end || 
		case when store3 is not null then 'store3,' end || 
		case when store4 is not null then 'store4,' end || 
		case when store5 is not null then 'store5' end 
	,',')
	mycsv
from tab1
/ 

Open in new window

0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39906653
Slight,
I'm assuming if a row has no data then he doesn't want to see said row.
select col6, rtrim(case when nvl(col1,'') <> '' then 'col1, ' else '' end || 
                    case when nvl(col2,'')<> '' then 'col2, ' else '' end ||
                    case when nvl(col3,'')<> '' then 'col3, ' else '' end ||
                    case when nvl(col4,'')<> '' then 'col4, ' else '' end ||
                    case when nvl(col5,'')<> '' then 'col5, ' else '' end ,',') "Cols"
from <table>
where 
nvl(col1,'') <> '' or 
nvl(col2,'') <> '' or 
nvl(col3,'') <> '' or 
nvl(col4,'') <> '' or 
nvl(col5,'') <> '' 

Open in new window

0
 
LVL 5

Author Comment

by:CalmSoul
ID: 39906841
thanks Kyle, let me see your solution
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39908011
>> then he doesn't want to see said row.

Unfortunately "nvl(col1,'') <> '' or ""  will not work in Oracle.

An empty string is considered a null value and nothing can be '=' or '!=' to a null.  You MUST use 'is null' and 'is not null'.

Try it:
select 'hello' from dual where nvl(null,'') = '';


If they have that requirement, personally I would do:
when coalesce(col1,col2,col3,col4,col5) is not null
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39908142
NVL converts the null to an empty string making it comparable . . . same thing as

isnull(column, '') != ''

no?
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39908160
>>no?

No.   Try it from what I posted with the DUAL table.

NVL converts a null to an actual value.  An empty string is the same thing as a null in Oracle and not an actual value.

Oracle Database treats a character value with a length of zero as null. However, do not use null to represent a numeric value of zero, because they are not equivalent.

http://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements005.htm#SQLRF30037
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39908179
Alright I stand corrected (it's been a few years since I worked with oracle),

change the

NVL (XX, '') <> ''
to NVL(XX, ' ') <> ' '


if there are more than one blank you may have to do:
/*Translate 2 or more blanks to one blank */
NVL( REGEXP_REPLACE(XX,'( ){2,}', ' '), ' ') <> ' '



As the original poster has said there are both null and blanks so just worrying about the null is not enough.
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 39909174
>>As the original poster has said there are both null and blanks so just worrying about the null is not enough.

Then "to NVL(XX, ' ') <> ' '"  will return a column with '           '.  Still not the requirement?

If you want to treat spaces in a column as 'no data' and a null then:
when coalesce(trim(col1),trim(col2),trim(col3),trim(col4),trim(col5)) is not null

regexp calls are very expensive and should only be used when necessary.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ORA-01008: not all variables bound. 6 38
help on oracle query 5 33
mysql joining from the same table 6 36
Oracle - SQL Parse String 5 20
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now