Hi Chris/Connor,
I have a below query output:
select tab1.usr, tab2.salary
from tab1
LEFT JOIN tab2 ON (tab1.usr = tab2.usr and tab1.name = tab2.name);
USR salary
----- -------
1111 5001
2222 NULL
3333 NULL
Since usr: 2222 not exists in tab2 I am trying to get the output like below:
USR salary
----- -------
1111 5001
2222 NULL
3333 'NOT FOUND'
Can you please help to modify my query above to achieve this please.
So you want to know if you joined to a row, but the column is null vs when there's no matching row in the outer joined table?
If so, check the values of the joined columns. If this is not null then display the column. Otherwise return the
You can do this in a case expression or decode:
create table tab1
(
usr number,
name varchar2(10)
);
create table tab2
(
usr number,
name varchar2(10),
salary number
);
insert into tab1
values (1111, 'Jerry');
insert into tab1
values (2222, 'John');
insert into tab1
values (3333, 'Scott');
insert into tab2
values (1111, 'Jerry', 5001);
insert into tab2
values (3333, 'Jerry', NULL);
commit;
select tab1.usr, tab2.usr ,
case
when tab2.usr is not null then
to_char(tab2.salary)
else
'NOT FOUND'
end case_sal,
decode( tab2.usr , null , 'NOT FOUND', to_char(tab2.salary) ) dec_sal
from tab1
left join tab2
on tab1.usr = tab2.usr;
USR USR CASE_SAL DEC_SAL
1111 1111 5001 5001
3333 3333
2222 NOT FOUND NOT FOUND
(The names for 3333 don't match, I'm guessing you these are supposed to be the same or you just wanted to join on usr...)