Help with select

Asked

Viewed 144 times

3

I’m using the remote select down 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 he returns me 4 fields: team name, professional name, citizen name and date of the form.

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

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

I hope you understand.

  • I believe @Sorack’s response is the most appropriate to your case :)

3 answers

6


Use the LEFT JOIN together with the clause CASE:

    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'
  • the table g uses the f and the table f uses the g, I couldn’t adjust that :/

  • @Italorodrigo if one is using the other the keys are probably incorrect

  • @Italorodrigo the ideal is to use an indentation and make the links more readable with the use of Join, it will facilitate your life :). What Sorack answered is correct, use the left Join allied to the case that will be successful.

  • This database is from a program of the health ministry, I’m getting some information from it to generate a report. it has 726 kkkk tables. but I’m trying here.

3

You can use Case When by selecting it and removing it from the '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 haven’t tested this code, but I believe it solves!

0

In this case you can make one LEFT JOIN, which makes the result merge keeping the line if the join is not positive. And when selecting the field use the 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';
  • 1

    There was an error in the line where Join is: Hint: Há uma entrada para tabela "a", mas ela não pode ser referenciada desta parte da consulta.&#xA;Character: 345

Browser other questions tagged

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