Home » SQL & PL/SQL » SQL & PL/SQL » How to remove the NULL values from SELECT clause
How to remove the NULL values from SELECT clause [message #634390] Mon, 09 March 2015 07:02 Go to next message
samiran_cts
Messages: 52
Registered: January 2012
Member
I am creating one query:

select col1,null col2,null col3 from test1
union all
select null,col2,null col3 from test2
union all
select null,null, col3 from test2;


The query displaying the records as:
col1 col2 col3
ABC NULL NULL
NULL PQR NULL
NULL LMP NULL
NULL NULL CCC

But I want to remove the NULL and want to display as
col1 col2 col3
ABC PQR CCC
NULL LMP NULL
Re: How to remove the NULL values from SELECT clause [message #634391 is a reply to message #634390] Mon, 09 March 2015 07:05 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
You need to define some rules. For example, why this
col1 col2 col3
ABC PQR CCC
NULL LMP NULL

rather than this:
col1 col2 col3
ABC LMP CCC
NULL PQR NULL

Re: How to remove the NULL values from SELECT clause [message #634395 is a reply to message #634390] Mon, 09 March 2015 07:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

From one of your previous topics:

Michel Cadot wrote on Wed, 20 June 2012 15:29
Michel Cadot wrote on Wed, 20 June 2012 13:02
...
Before, Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version, with 4 decimals.
...
Regards
Michel



With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Also feedback and thank people for their time and help otherwise you'll no more have any help.

[Updated on: Mon, 09 March 2015 07:44]

Report message to a moderator

Re: How to remove the NULL values from SELECT clause [message #634411 is a reply to message #634390] Mon, 09 March 2015 16:06 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
The following arbitrarily orders by rowid, but you could order by any other column if you went the vaues in a column in a specific order. I also used 3 tables, assuming that your last test2 was actually test3, but if your data for col3 is actually in test2, then you could use test2 instead.

SCOTT@orcl12c> -- test data:
SCOTT@orcl12c> create table test1 (col1 varchar2(4))
  2  /

Table created.

SCOTT@orcl12c> insert into test1 values ('ABC')
  2  /

1 row created.

SCOTT@orcl12c> create table test2 (col2 varchar2(4))
  2  /

Table created.

SCOTT@orcl12c> insert into test2 values ('PQR')
  2  /

1 row created.

SCOTT@orcl12c> insert into test2 values ('LMP')
  2  /

1 row created.

SCOTT@orcl12c> create table test3 (col3 varchar2(4))
  2  /

Table created.

SCOTT@orcl12c> insert into test3 values ('CCC')
  2  /

1 row created.

SCOTT@orcl12c> -- original query:
SCOTT@orcl12c> set null NULL
SCOTT@orcl12c> select col1,null col2,null col3 from test1
  2  union all
  3  select null,col2,null col3 from test2
  4  union all
  5  select null,null, col3 from test3
  6  /

COL1 COL2 COL3
---- ---- ----
ABC  NULL NULL
NULL PQR  NULL
NULL LMP  NULL
NULL NULL CCC

4 rows selected.

SCOTT@orcl12c> -- revised query:
SCOTT@orcl12c> select t1.col1, t2.col2, t3.col3
  2  from   (select col1, rownum rn
  3  	     from   test1
  4  	     order  by rowid) t1
  5  full outer join
  6  	    (select col2, rownum rn
  7  	     from   test2
  8  	     order  by rowid) t2
  9  on (t1.rn = t2.rn)
 10  full outer join
 11  	    (select col3, rownum rn
 12  	     from   test3
 13  	     order  by rowid) t3
 14  on (t2.rn = t3.rn)
 15  /

COL1 COL2 COL3
---- ---- ----
ABC  PQR  CCC
NULL LMP  NULL

2 rows selected.

SCOTT@orcl12c> 



Re: How to remove the NULL values from SELECT clause [message #634421 is a reply to message #634411] Tue, 10 March 2015 01:31 Go to previous messageGo to next message
samiran_cts
Messages: 52
Registered: January 2012
Member
create table primary_tbl
(profile id number,
primay_cust varchar2(10),
Primary_bus varchar2(10)
)/

insert into primary_tbl values(100,'Testing','Developer') /
insert into primary_tbl values(101,'Testing1','Developer1') /

create table sec_bus_tbl
(profile id number,
sec_bus varchar2(10)
)/

insert into sec_bus_tbl values(100,'Administrator') /
insert into sec_bus_tbl values(100,'Business') /
insert into sec_bus_tbl values(101,'Business1') /

create table sec_cust_tbl
(profile id number,
sec_cust varchar2(10)
)/

insert into sec_cust_tbl values(100,'Test') /
insert into sec_cust_tbl values(100,'Test_test') /
insert into sec_cust_tbl values(101,'Test_test1') /


I want the output for profile id 100 in this format:

primay_cust    Primary_bus   sec_bus          sec_cust
-----------------------------------------------------
Testing			Developer    Administrator     Test
NULL			NULL			Business	  Test_test


Thank you in advance,

Regards,
Samiran
Re: How to remove the NULL values from SELECT clause [message #634423 is a reply to message #634411] Tue, 10 March 2015 01:35 Go to previous messageGo to next message
samiran_cts
Messages: 52
Registered: January 2012
Member
Thank you for all the help.
Re: How to remove the NULL values from SELECT clause [message #634424 is a reply to message #634421] Tue, 10 March 2015 01:42 Go to previous messageGo to next message
samiran_cts
Messages: 52
Registered: January 2012
Member
Sorry I made mistake while creating the script. Please ignore my previous script. I am very sorry.

create table primary_tbl
(profile_id number,
primay_cust varchar2(50),
Primary_bus varchar2(50)
)/

insert into primary_tbl values(100,'Testing','Developer') /
insert into primary_tbl values(101,'Testing1','Developer1') /

create table sec_bus_tbl
(profile_id number,
sec_bus varchar2(50)
)/

insert into sec_bus_tbl values(100,'Administrator') /
insert into sec_bus_tbl values(100,'Business') /
insert into sec_bus_tbl values(101,'Business1') /

create table sec_cust_tbl
(profile_id number,
sec_cust varchar2(50)
)/

insert into sec_cust_tbl values(100,'Test') /
insert into sec_cust_tbl values(100,'Test_test') /
insert into sec_cust_tbl values(101,'Test_test1') /


The output I want:
primay_cust    Primary_bus   sec_bus          sec_cust
-----------------------------------------------------
Testing			Developer    Administrator     Test
NULL			NULL			Business	  Test_test


Thanks ,
Samiran
Re: How to remove the NULL values from SELECT clause [message #634432 is a reply to message #634424] Tue, 10 March 2015 02:14 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Read and answer John's post.
And you test case is still invalid: '/' should be alone on a new line or replace it by ';'.
And align the columns in your result; don't you see what a mess it is?
And where does 'Testing' come from?
And why there are no 'Test_test1', 'Business1', 'Developer1' in your result?
It is not sufficient to say "I want this output", you MUST explain the rules to go to this output.

[Updated on: Tue, 10 March 2015 02:15]

Report message to a moderator

Re: How to remove the NULL values from SELECT clause [message #634437 is a reply to message #634432] Tue, 10 March 2015 03:04 Go to previous messageGo to next message
samiran_cts
Messages: 52
Registered: January 2012
Member
Basically I need a single query that will display primay_cust,Primary_bus,sec_bus,sec_cust for a profile id. The primay_cust,Primary_bus is present in the table primary_tbl, the sec_bus is present in the table sec_bus_tbl and sec_cust is present in the table sec_cust_tbl. I am again giving the script for the table structure.
create table primary_tbl
(profile_id number,
primay_cust varchar2(50),
Primary_bus varchar2(50)
);

insert into primary_tbl values(100,'Testing','Developer') ;
insert into primary_tbl values(101,'Testing1','Developer1') ;

create table sec_bus_tbl
(profile_id number,
sec_bus varchar2(50)
);

insert into sec_bus_tbl values(100,'Administrator') ;
insert into sec_bus_tbl values(100,'Business') ;
insert into sec_bus_tbl values(101,'Business1') ;

create table sec_cust_tbl
(profile_id number,
sec_cust varchar2(50)
);

insert into sec_cust_tbl values(100,'Test') ;
insert into sec_cust_tbl values(100,'Test_test') ;
insert into sec_cust_tbl values(101,'Test_test1') ;


Now I need to display the result for profile id 100. The Output should look like:
primay_cust    Primary_bus      sec_bus          sec_cust
----------------------------------------------------------------
Testing			Developer    Administrator      Test
NULL			NULL	     Business	     Test_test


I tried in this way:
select t1.primay_cust,t1.Primary_bus, t2.sec_bus, t3.sec_cust
   from   (select primay_cust,Primary_bus, rownum rn
   	     from   primary_tbl where profile_id=100
   	     order  by rowid) t1
   full outer join
   	    (select sec_bus, rownum rn
   	     from   sec_bus_tbl where profile_id=100
   	     order  by rowid) t2
   on (t1.rn = t2.rn)
   full outer join
   	    (select sec_cust, rownum rn
   	     from   sec_cust_tbl where profile_id=100
   	     order  by rowid) t3
   on (t2.rn = t3.rn)


I am getting the desired result but I don't want to use profile_id 100 3 times. So any better way to do so.

Thanks in advance

Regards,
Samiran
Re: How to remove the NULL values from SELECT clause [message #634439 is a reply to message #634437] Tue, 10 March 2015 03:14 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Put the WHERE clause on the outer query, anyway Oracle optimizer will push it in the inner ones.

You don't need any ROWNUM and ORDER BY clause, but you have to join on 'profile_id'.
And if "primary" is mandatory the FULL OUTER joins and LEFT OUTER ones.

Re: How to remove the NULL values from SELECT clause [message #634442 is a reply to message #634439] Tue, 10 March 2015 03:23 Go to previous messageGo to next message
samiran_cts
Messages: 52
Registered: January 2012
Member
Thanks Michel for your feedback.
But if I join on the basis of profile id, i am not getting the desired result.
Re: How to remove the NULL values from SELECT clause [message #634445 is a reply to message #634442] Tue, 10 March 2015 03:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Show us what you do and get.
What is the purpose of profile_id if you don't use it?
Once again:
why is this
Administrator      Test
Business	     Test_test

And not
Administrator      Test_test
Business	     Test

How do you define that? What is the rule?

Re: How to remove the NULL values from SELECT clause [message #634447 is a reply to message #634445] Tue, 10 March 2015 03:36 Go to previous messageGo to next message
samiran_cts
Messages: 52
Registered: January 2012
Member
In the table there could be multiple Profile_id.For simplicity I inserted records for 100 and 101 profile_id. For each profile_id there are multiple primay_cust,Primary_bus,sec_bus,sec_cust.

Thanks,
Samiran
Re: How to remove the NULL values from SELECT clause [message #634448 is a reply to message #634447] Tue, 10 March 2015 03:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

These are important things to know; remember we know nothing about your model but what you tell us.
Now answer to my previous question.

Re: How to remove the NULL values from SELECT clause [message #634450 is a reply to message #634448] Tue, 10 March 2015 04:11 Go to previous messageGo to next message
samiran_cts
Messages: 52
Registered: January 2012
Member
I do understand Michel.
Regarding your question. It can be define any order you mentioned.I joined on the basis of profile id also.
SELECT T1.PRIMAY_CUST,T1.PRIMARY_BUS, T2.SEC_BUS, T3.SEC_CUST
   from   (select primay_cust,Primary_bus,profile_id
   	     from   primary_tbl 
   	     order  by rowid) t1
   inner join
   	    (select sec_bus, profile_id
   	     from   sec_bus_tbl 
   	     order  by rowid) t2
   ON (T1.PROFILE_ID = T2.PROFILE_ID)
   inner join
   	    (select sec_cust,profile_id
   	     from   sec_cust_tbl
   	     order  by rowid) t3
   ON (T2.profile_id = T3.profile_id) and t1.profile_id=100;;


But I am not getting the desired output. I am getting duplicate records.

Thanks !!
Samiran
Re: How to remove the NULL values from SELECT clause [message #634455 is a reply to message #634450] Tue, 10 March 2015 04:33 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

If any order apply you don't need any ORDER BY clause in your query.
You also have to join on something else that uniquely define the row in each select.
And the latest test (t1.profile_id=100) is a WHERE clause of the outer query.

Something like:
SQL> SELECT T1.PRIMAY_CUST,T1.PRIMARY_BUS, T2.SEC_BUS, T3.SEC_CUST
  2     from   (select primay_cust,Primary_bus,profile_id,
  3                    row_number() over (partition by profile_id order by null) rn
  4                  from   primary_tbl ) t1
  5     full outer join
  6                 (select sec_bus, profile_id,
  7                    row_number() over (partition by profile_id order by null) rn
  8                  from   sec_bus_tbl) t2
  9     ON (T1.PROFILE_ID = T2.PROFILE_ID and t1.rn = t2.rn )
 10     full outer join
 11                 (select sec_cust,profile_id,
 12                    row_number() over (partition by profile_id order by null) rn
 13                  from   sec_cust_tbl) t3
 14     ON (nvl(T1.PROFILE_ID,T2.profile_id) = T3.profile_id
 15         and nvl(t1.rn,t2.rn) = t3.rn)
 16  where coalesce(t1.profile_id,t2.profile_id,t3.profile_id)=100
 17  /
PRIMAY_CUST     PRIMARY_BUS     SEC_BUS         SEC_CUST
--------------- --------------- --------------- ---------------
Testing         Developer       Administrator   Test
                                Business        Test_test

2 rows selected.

You have to use analytic function because you want only one test on profile_id which is less efficient than 3.

Re: How to remove the NULL values from SELECT clause [message #634461 is a reply to message #634455] Tue, 10 March 2015 05:25 Go to previous message
samiran_cts
Messages: 52
Registered: January 2012
Member
Thank you so much !!!!!! Smile Smile
Previous Topic: How to restrict the inserting a record through a oracle trigger
Next Topic: Complex comparision query
Goto Forum:
  


Current Time: Thu Apr 25 14:39:33 CDT 2024