Help with select

3

I'm using the command select below:

select
f.no_equipe,
h.no_pessoa_fisica,
a.no_cidadao,
d.dt_ficha
from
tb_cds_cad_individual a,
tb_cds_atend_individual b,
rl_cds_atend_individual_ciap c,
tb_cds_ficha_atend_individual d,
tb_equipe f,
tb_cds_prof g,
tb_pessoa_fisica h
where f.nu_ine = g.nu_ine
and g.nu_cns = h.nu_cns
and d.co_cds_prof = g.co_seq_cds_prof
and a.nu_cns_cidadao = b.nu_cartao_sus
and b.co_seq_cds_atend_individual = c.co_cds_atend_individual
and b.co_cds_ficha_atend_individual = d.co_seq_cds_ficha_atend_indivdl
and c.co_ciap = 727
and d.dt_ficha >= '2017-08-01'
and d.dt_ficha <= '2017-08-30'

In this code it returns me 4 fields: team name, professional's name, citizen's name and date of the card.

My problem is in line and a.nu_cns_cidadao = b.nu_cartao_sus where it only shows the line if the sus card number is equal to the number registered in another table.

I want to change the code so that even if the and a.nu_cns_cidadao = b.nu_cartao_sus condition is not true, it lists the result and instead appears in the citizen name a.no_cidadao something like "NO NAME" appears.

I hope you have understood.

    
asked by anonymous 06.09.2017 / 19:41

3 answers

6

Use LEFT JOIN in conjunction with CASE clause:

    SELECT f.no_equipe,
       h.no_pessoa_fisica,
       CASE
         WHEN a.no_cidadao IS NULL THEN 'GESTANTE NAO CADASTRADA'
         ELSE a.no_cidadao
       END AS no_cidadao,
       d.dt_ficha
  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-08-01'
 AND d.dt_ficha <= '2017-08-30'
    
06.09.2017 / 19:51
3

You can use Case When by selecting it and removing it from 'where', as below:

select
    f.no_equipe,
    h.no_pessoa_fisica,
case when
     a.nu_cns_cidadao = b.nu_cartao_sus 
then a.nu_cns_cidadao
else 'SEM NOME' end,
     d.dt_ficha
from
   tb_cds_cad_individual a,
   tb_cds_atend_individual b,
   rl_cds_atend_individual_ciap c,
   tb_cds_ficha_atend_individual d,
   tb_equipe f,
   tb_cds_prof g,
   tb_pessoa_fisica h
where f.nu_ine = g.nu_ine
   and g.nu_cns = h.nu_cns
   and d.co_cds_prof = g.co_seq_cds_prof
   and b.co_seq_cds_atend_individual = c.co_cds_atend_individual
   and b.co_cds_ficha_atend_individual = d.co_seq_cds_ficha_atend_indivdl
   and c.co_ciap = 727
   and d.dt_ficha >= '2017-08-01'
   and d.dt_ficha <= '2017-08-30'

I have not tested this code, but I believe it will solve!

    
06.09.2017 / 19:49
0

In this case you can make a LEFT JOIN , which makes the results join keeping the line if the join is not positive. And when selecting the field use COALESCE to return a second value if the first is NULL . Example:

select
    f.no_equipe,
    h.no_pessoa_fisica,
    COALESCE(a.no_cidadao, 'SEM NOME') AS no_cidadao,
    d.dt_ficha
from
    tb_cds_cad_individual a,
    rl_cds_atend_individual_ciap c,
    tb_cds_ficha_atend_individual d,
    tb_equipe f,
    tb_cds_prof g,
    tb_pessoa_fisica h
    LEFT JOIN tb_cds_atend_individual b ON a.nu_cns_cidadao = b.nu_cartao_sus
where
    f.nu_ine = g.nu_ine
    and g.nu_cns = h.nu_cns
    and d.co_cds_prof = g.co_seq_cds_prof
    and b.co_seq_cds_atend_individual = c.co_cds_atend_individual
    and b.co_cds_ficha_atend_individual = d.co_seq_cds_ficha_atend_indivdl
    and c.co_ciap = 727
    and d.dt_ficha >= '2017-08-01'
    and d.dt_ficha <= '2017-08-30';
    
06.09.2017 / 19:46