Problema quando queremos realizar um Join de duas tabelas e justamente a coluna de junção na tabela da esquerda não tem alguns valores que existem na tabela na direita.
|
Tabela: esquerda |
|
Tabela: direita |
||
|
dia |
nome |
|
dia |
sobrenome |
|
1-Domingo |
Wilson |
|
1-Domingo |
Koba |
|
3-Terça |
Felipe |
|
2-Segunda |
Perez |
|
4-Quarta |
Bruno |
|
4-Quarta |
Garcia |
|
6-Sexta |
Robson |
|
5-Quinta |
Pinheiro |
Quando executados um join via PROC SQL de FULL JOIN o resultado será como abaixo:
Código:
PROC SQL;
CREATE table JOIN_SEM_COALESCE as
select a.dia, a.primeironome, b.sobrenome
from nome a full join sobrenome b
on a.dia=b.dia;
Quit;
Resultado:
|
Obs |
dia |
primeironome |
sobrenome |
|
1 |
1-Domingo |
Wilson |
Koba |
|
2 |
|
|
Perez |
|
3 |
3-Terça |
Felipe |
|
|
4 |
4-Quarta |
Bruno |
Garcia |
|
5 |
|
|
Pinheiro |
|
6 |
6-Sexta |
Robson |
|
Notem que as linhas 2 e 5 estão faltando as informações “2-Segunda” e “5-Quinta” respectivamente.
Utilizando a função COALESCE, que vai nos ajudar a colocar o primeiro valor não nulo de uma lista de valores, nos permite evitar este tipo de problema.
SAS Help Center: COALESCE Function
Código com função COALESCE
PROC SQL;
CREATE table JOIN_COM_COALESCE as
select coalesce(a.dia,b.dia), a.primeironome, b.sobrenome
from nome a full join sobrenome b
on a.dia=b.dia;
quit;
Resultado:
|
Obs |
dia |
primeironome |
sobrenome |
|
1 |
1-Domingo |
Wilson |
Koba |
|
2 |
2-Segunda |
|
Perez |
|
3 |
3-Terça |
Felipe |
|
|
4 |
4-Quarta |
Bruno |
Garcia |
|
5 |
5-Quinta |
|
Pinheiro |
|
6 |
6-Sexta |
Robson |
|
Nearly 200 sessions are now available on demand in the Innovate Hub.
Watch Now →
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.