Skip to Main Content
  • Questions
  • How can I show result sets like this?

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Derek.

Asked: November 28, 2018 - 3:40 am UTC

Last updated: December 10, 2018 - 3:16 pm UTC

Version: 12

Viewed 1000+ times

You Asked

Hi Tom,

I have two tables. One is test and another is test1. Each table has two columns, record_id and c1 and both are varchar2(10).

Test content is below

record_id  c1 
-----------------
1         null
2         3
3         +
4         null

record_id c1
------ ---------
1          2
2         null
3          +
4         null


I would like the result like below

test.record_id  test.c1      test1.record_id test1.c1
------------------------------------------------------
1              null                       1         2
2              3                          2         null


which means if test.c1=test1.c1, no show. If both are null no show. only show test.c1 test1.c1 if one side are null values, how can I construct my query?

Kind Regards,

Derek




and Chris said...

Join where the column is not null in one table and null in the other or vice-versa:

create table t1 (
  id int,
  c1 varchar2(1)
);
create table t2 (
  id int,
  c1 varchar2(1)
);

insert into t1 values ( 1, null );
insert into t1 values ( 2, 3 );
insert into t1 values ( 3, '+' );
insert into t1 values ( 4, null );

insert into t2 values ( 1, 2 );
insert into t2 values ( 2, null );
insert into t2 values ( 3, '+' );
insert into t2 values ( 4, null );
commit;

select * from t1
join   t2
on     t1.id = t2.id
and    ( 
  ( t1.c1 is null and t2.c1 is not null) or 
  ( t1.c1 is not null and t2.c1 is null) 
);

ID   C1       ID   C1       
   1 <null>      1 2        
   2 3           2 <null>   

Rating

  (8 ratings)

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

Comments

Further question

Derek Xu, November 29, 2018 - 12:07 am UTC

Hi Chris,

Thanks a lot. If t1 c1 has so many special characters such as '+','#','*', I hope those special characters without change in the t2 c1 column based on the same id. How can I come out a SQL for this?

Kind Regards,

Derek

further questions

Derek Xu, November 29, 2018 - 12:18 am UTC

Hi Chris,

Sorry, let me rephrase my question. My purpose is table T1 column c1 has null values, special characters and numeric string and normal characters such as abc,cde,etc.

I transferred the t1.c1 to t2.c1, I would like to confirm those null values, special characters,and numeric strings no change based on the same id. Only normal character strings of the column changed from t1 to t2.

Can one sql do this for the first situation?

Kind Regards,

Derek
Chris Saxon
November 29, 2018 - 9:37 am UTC

I'm not sure what you're trying to do here. We need more examples.

Please post sample data covering all cases in the form of create table + inserts (like in my answer). And the output you expect based on these data.

Derek Xu, November 29, 2018 - 4:40 pm UTC

Hi Chris,

Thank you for your reply. Sorry probably I didn't

I have two tables like SQLs below,

create table t1 (id char(4),c1 char(4));
create table t2 (id char(4),c1 char(4));

insert into t1 values (1,null);
insert into t2 values (1,null);

insert into t1 values (2,9);
insert into t2 values (2,9);

insert into t1 values (3,null);
insert into t2 values (3,8);

insert into t1 values (4,'+');
insert into t2 values (4,'+');

insert into t1 values (5,'*');
insert into t2 values (5,'!');

insert into t1 values (6,'a');
insert into t2 values (6,null);

insert into t1 values (7,'b');
insert into t2 values (7,'b');

insert into t1 values (8,'c');
insert into t2 values (8,'d');

I would the SQLs based on the same id like,

Scenario 1:

If t1.c1 is numeric, the query will return equal or not equal c1.

Scenario 2:

If t1.c1 is null, the query will return equal or not equal, I treat if both are null, they should be return.

Scenario 3:

If t1.c1 is special character, the query will return equal or not equal c1.

Scenario 4:

If t1.c1 is alpha characters, the query will be return equal or not equal c1.

Each scenario can be one sql.

Kind Regards,

Derek
Chris Saxon
November 29, 2018 - 5:19 pm UTC

So what exactly do you expect the result to be based on these data?

Derek Xu, November 29, 2018 - 9:50 pm UTC

Hi Chris,

Actually, my purpose is I have a table t1 (id char(2), c1 varchar2(10)). c1 has the values null, special characters like '*','+', '-' and numeric and alpha values.

I created t2 table the same structure like t1. I used a plsql to transferred the data from t1 to t2. I would like to check all data transferred correctly based on the different patterns .

Kind Regards,

Derek
Chris Saxon
November 30, 2018 - 3:27 pm UTC

OK, now I'm confused. What exactly are you trying to do? What is your input and what is the output you want based on these data?

Derek Xu, November 30, 2018 - 4:41 pm UTC

Thanks Chris for your reply and I will prepare my scripts to show my ideas then update you later.

Kind Regards,

Derek

Maybe this?

paul, November 30, 2018 - 5:05 pm UTC

Doesn't seem like the datatype matters.
maybe something like this?
https://livesql.oracle.com/apex/livesql/s/hmcg74tyhnfah17qvwctp0rxf


select  
  t1.id,  
  t1.c1 "one", 
  t2.c1 "two", 
  case 
  when t1.c1= t2.c1 then 'EQ'
  when t1.c1 is null and t2.c1 is null then 'EQ'
  end "a" 
from  
  t1, t2 
 where t1.id=t2.id


ID one two a
1     -   -  EQ
2    9    9    EQ
3     -  8     - 
4    +    +    EQ
5    *    !     - 
6    a     -   - 
7    b    b    EQ
8    c    d     - 

Chris Saxon
December 10, 2018 - 3:16 pm UTC

Possibly, we need Derek to confirm...

Could it be this simple?

Chuck Jolley, November 30, 2018 - 7:54 pm UTC

select *
  from t1, t2
 where t1.id = t2.id
   and nvl(t1.c1, 'null') <> nvl(t2.c1, 'null')


Chris Saxon
December 10, 2018 - 3:16 pm UTC

Possibly, we need Derek to confirm...

To Derek

Gh, December 02, 2018 - 7:00 am UTC

1. Create your test2 table with the default clause on c1 give it a Martian default value ie create table test2 ( Id number, c1 char(30) default 'DonaldTrump' )
Then use a simple Minus operator between test1 and test2.
If you desire the whole informations from both tables you could use the Lateral or Cross apply..

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.