Help with Join in two different selects

Asked

Viewed 710 times

2

I have two select commands (a bit complex).

First code:

select 
A.no_cidadao NOME,
F.no_equipe UNIDADE
from 
tb_cds_cidadao_resposta E,
tb_cds_cad_individual A,
tb_cds_prof as G,
tb_pessoa_fisica as H,
tb_equipe as F
where
A.co_seq_cds_cad_individual = E.co_cds_cad_individual
and G.nu_cns = H.nu_cns
and G.co_seq_cds_prof = A.co_cds_prof_cadastrante
and E.co_pergunta = 19
and E.st_resposta = 1
and G.nu_ine = F.nu_ine
group by 
A.no_cidadao, F.no_equipe
order by 
F.no_equipe, A.no_cidadao

get back to me:

unidade - nome
unit 1  - maria
unit 1  - severina
unit 2   - renata

Second code:

SELECT f.no_equipe UNIDADE,
   h.no_pessoa_fisica PROFISSIONAL,
   CASE
     WHEN a.no_cidadao IS NULL THEN 'GESTANTE NAO CADASTRADA'
     ELSE a.no_cidadao
   END AS NOME,
   d.dt_ficha as DATA
  FROM 
   tb_cds_atend_individual b 
   LEFT JOIN tb_cds_cad_individual a ON a.nu_cns_cidadao = b.nu_cartao_sus
   LEFT JOIN rl_cds_atend_individual_ciap c ON b.co_seq_cds_atend_individual = c.co_cds_atend_individual
   LEFT JOIN tb_cds_ficha_atend_individual d ON b.co_cds_ficha_atend_individual = d.co_seq_cds_ficha_atend_indivdl
   LEFT JOIN tb_cds_prof g ON d.co_cds_prof = g.co_seq_cds_prof
   LEFT JOIN tb_equipe f ON f.nu_ine = g.nu_ine
   LEFT JOIN tb_pessoa_fisica h ON g.nu_cns = h.nu_cns
   WHERE c.co_ciap = 727
   AND d.dt_ficha >= '2017-10-01'
   AND d.dt_ficha <= '2017-10-31'
   order by no_equipe

this returns me the fields unidade - profissional - nome - data

there are data in Code 1 that is not in Code 2 and vice versa. what I need to do is unite the two tables into one:

unidade - nome            - profissional - data
unit 1  - maria           - dr xico      - 31/10/2017
unit 1  - NAO CADASTRADA  - dr xico      - 25/10/2017 //não achou o nome no codigo 1
unit 1  - severina        - dr xico      - 28/10/2017
unit 2  - renata          - SEM CONSULTA -     X      //não achou o nome no codigo 2

2 answers

1


See if the format below helps you. I didn’t make much modification in your query, I just used the two informed querys as a subselect, using a "union all" in them.

SELECT X.UNIDADE,
       X.PROFISSIONAL,
       X.NOME,
       X.DATA

FROM (
SELECT f.no_equipe as UNIDADE,
       h.no_pessoa_fisica  as PROFISSIONAL,
   CASE
     WHEN a.no_cidadao IS NULL THEN 'GESTANTE NAO CADASTRADA'
     ELSE a.no_cidadao
   END AS NOME,
   d.dt_ficha as DATA
  FROM 
   tb_cds_atend_individual b 
   LEFT JOIN tb_cds_cad_individual a ON a.nu_cns_cidadao = b.nu_cartao_sus
   LEFT JOIN rl_cds_atend_individual_ciap c ON b.co_seq_cds_atend_individual = c.co_cds_atend_individual
   LEFT JOIN tb_cds_ficha_atend_individual d ON b.co_cds_ficha_atend_individual = d.co_seq_cds_ficha_atend_indivdl
   LEFT JOIN tb_cds_prof g ON d.co_cds_prof = g.co_seq_cds_prof
   LEFT JOIN tb_equipe f ON f.nu_ine = g.nu_ine
   LEFT JOIN tb_pessoa_fisica h ON g.nu_cns = h.nu_cns
   WHERE c.co_ciap = 727
   AND d.dt_ficha >= '2017-10-01'
   AND d.dt_ficha <= '2017-10-31'
   -- order by no_equipe

   UNION ALL

select 
F.no_equipe as UNIDADE,
null as PROFISSIONAL,
A.no_cidadao as NOME,
null as DATA

from 
tb_cds_cidadao_resposta E,
tb_cds_cad_individual A,
tb_cds_prof as G,
tb_pessoa_fisica as H,
tb_equipe as F
where
A.co_seq_cds_cad_individual = E.co_cds_cad_individual
and G.nu_cns = H.nu_cns
and G.co_seq_cds_prof = A.co_cds_prof_cadastrante
and E.co_pergunta = 19
and E.st_resposta = 1
and G.nu_ine = F.nu_ine
group by 
A.no_cidadao, F.no_equipe
--order by 
--F.no_equipe, A.no_cidadao 
 ) X

ORDER BY X.UNIDADE, x.NOME

0

I did not go into your model, to better understand what you need, paying attention only to the need to relate two distinct selects.

One option is to use temporary tables:

with tabelaX as (
select 
A.no_cidadao NOME,
F.no_equipe UNIDADE
from 
tb_cds_cidadao_resposta E,
tb_cds_cad_individual A,
tb_cds_prof as G,
tb_pessoa_fisica as H,
tb_equipe as F
where
A.co_seq_cds_cad_individual = E.co_cds_cad_individual
and G.nu_cns = H.nu_cns
and G.co_seq_cds_prof = A.co_cds_prof_cadastrante
and E.co_pergunta = 19
and E.st_resposta = 1
and G.nu_ine = F.nu_ine
group by 
A.no_cidadao, F.no_equipe
order by 
F.no_equipe, A.no_cidadao), 

tabelaY as (

SELECT f.no_equipe UNIDADE,
   h.no_pessoa_fisica PROFISSIONAL,
   COALESCE(a.no_cidadao,'Gestante não cadastrada') as nome, --não precisa do case para colunas nulas, utilize o coalesce
   d.dt_ficha as DATA
  FROM 
   tb_cds_atend_individual b 
   LEFT JOIN tb_cds_cad_individual a ON a.nu_cns_cidadao = b.nu_cartao_sus
   LEFT JOIN rl_cds_atend_individual_ciap c ON b.co_seq_cds_atend_individual = c.co_cds_atend_individual
   LEFT JOIN tb_cds_ficha_atend_individual d ON b.co_cds_ficha_atend_individual = d.co_seq_cds_ficha_atend_indivdl
   LEFT JOIN tb_cds_prof g ON d.co_cds_prof = g.co_seq_cds_prof
   LEFT JOIN tb_equipe f ON f.nu_ine = g.nu_ine
   LEFT JOIN tb_pessoa_fisica h ON g.nu_cns = h.nu_cns
   WHERE c.co_ciap = 727
   AND d.dt_ficha >= '2017-10-01'
   AND d.dt_ficha <= '2017-10-31'
   order by no_equipe)

Select
* 
from TabelaX x 
left outer join TabelaY Y on Y.nome = x.nome or (y.nome is null and x.unidade = y.unidade)

Note the issue of Coalesce for column name.

Maybe if you put the base model, you can better understand and even suggest another (better) solution to the problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.