Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

cristiansuzuki

Condição para select no Oracle

Recommended Posts

Bom dia pessoal!

Sou novo na parte de sintaxe SQL e preciso de um auxílio.

Estou utilizando Oracle e estou com o seguinte problema:

Preciso selecionar somente os pacientes (PAC.PAC_NOME) que fizeram respectivamente exames nas categorias C e E ( CTF.CTF_CATEG in ( 'C','E' ) ).

Ocorre que o select está trazendo os pacientes que fizeram C e E mas também trás os que fizeram somente C e somente E. Como faço uma condição para trazer os pacientes que fizeram C e E respectivamente dentro da data informada?

Segue o código que fiz:


SELECT PAC.PAC_NOME,

SMK.SMK_NOME,

SUM ( SMM.SMM_QT ),

CTF.CTF_CATEG

FROM PAC, SMM, SMK, CTF, CNV CV, STR, ESP, OSM, ESM, PSV

WHERE ( SMM.SMM_SFAT <> 'C' ) AND

( OSM.OSM_SERIE = SMM.SMM_OSM_SERIE ) AND

( OSM.OSM_NUM = SMM.SMM_OSM ) AND

( ( CV.CNV_COD = NVL ( SMM.SMM_CNV_COD, OSM.OSM_CNV ) ) ) AND

( STR.STR_COD = SMM.SMM_STR ) AND

( SMK.SMK_TIPO = SMM.SMM_TPCOD ) AND

( SMK.SMK_COD = SMM.SMM_COD ) AND

( SMK.SMK_TIPO = CTF.CTF_TIPO ) AND

( SMK.SMK_CTF = CTF.CTF_COD ) AND

( OSM.OSM_PAC = PAC.PAC_REG ) AND

( PSV.PSV_COD = SMM.SMM_MED ) AND

( ESM.ESM_MED = PSV.PSV_COD ) AND

( ESP.ESP_COD = ESM.ESM_ESP ) AND

( ESM.ESM_DEFAULT = 'S' ) AND

( CV.CNV_COD not in ( 'ICB','44' ) ) AND

( STR.STR_NOME not in ( 'ICB','CEC' ) ) AND

( CTF.CTF_CATEG in ( 'C','E' ) ) AND

( ESP.ESP_COD <> 'ICB' ) AND

( SMM.SMM_VLR > 0 ) AND

( SMM.SMM_COD not in ( '1AUX','2AUX','3AUX' ) ) AND

( ( OSM.OSM_DTHR >= to_date ( '01/10/2016 00:00', 'dd/mm/yyyy hh24:mi' ) AND

OSM.OSM_DTHR < to_date ( '27/10/2016 00:00', 'dd/mm/yyyy hh24:mi' ) ) )

ORDER BY PAC.PAC_NOME ASC

Compartilhar este post


Link para o post
Compartilhar em outros sites


SELECT PAC.PAC_NOME,

SMK.SMK_NOME,

SUM ( SMM.SMM_QT ),

CTF.CTF_CATEG

FROM PAC, SMM, SMK, CTF, CNV CV, STR, ESP, OSM, ESM, PSV

WHERE ( SMM.SMM_SFAT <> 'C' ) AND

( OSM.OSM_SERIE = SMM.SMM_OSM_SERIE ) AND

( OSM.OSM_NUM = SMM.SMM_OSM ) AND

( ( CV.CNV_COD = NVL ( SMM.SMM_CNV_COD, OSM.OSM_CNV ) ) ) AND

( STR.STR_COD = SMM.SMM_STR ) AND

( SMK.SMK_TIPO = SMM.SMM_TPCOD ) AND

( SMK.SMK_COD = SMM.SMM_COD ) AND

( SMK.SMK_TIPO = CTF.CTF_TIPO ) AND

( SMK.SMK_CTF = CTF.CTF_COD ) AND

( OSM.OSM_PAC = PAC.PAC_REG ) AND

( PSV.PSV_COD = SMM.SMM_MED ) AND

( ESM.ESM_MED = PSV.PSV_COD ) AND

( ESP.ESP_COD = ESM.ESM_ESP ) AND

( ESM.ESM_DEFAULT = 'S' ) AND

( CV.CNV_COD not in ( 'ICB','44' ) ) AND

( STR.STR_NOME not in ( 'ICB','CEC' ) ) AND

( CTF.CTF_CATEG in ( 'C','E' ) ) AND

( ESP.ESP_COD <> 'ICB' ) AND

( SMM.SMM_VLR > 0 ) AND

( SMM.SMM_COD not in ( '1AUX','2AUX','3AUX' ) ) AND

( ( OSM.OSM_DTHR >= to_date ( '01/10/2016 00:00', 'dd/mm/yyyy hh24:mi' ) AND

OSM.OSM_DTHR < to_date ( '27/10/2016 00:00', 'dd/mm/yyyy hh24:mi' ) ) )

group by PAC.PAC_NOME,

SMK.SMK_NOME,

CTF.CTF_CATEG

having count(distinct CTF.CTF_CATEG) = 2-- distintos = 2 => fez ambos

ORDER BY PAC.PAC_NOME ASC

Compartilhar este post


Link para o post
Compartilhar em outros sites

Obrigado pela ajuda.

Eu inseri as suas sugestões no código mas agora não trás informação nenhuma e nem da erro.

Se eu alterar a linha having count(distinct CTF.CTF_CATEG) = 2-- distintos = 2 => fez ambos e no lugar do primeiro 2 colocar 1 ele me trás informações mas ainda constam pacientes que fizeram apenas uma das categorias.

O que pode estar errado?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Tem pacientes que atendam a sua pesquisa (exames nas duas categorias) ?

Compartilhar este post


Link para o post
Compartilhar em outros sites


SELECT PAC.PAC_NOME,

SMK.SMK_NOME,

SUM ( SMM.SMM_QT )

FROM PAC, SMM, SMK, CTF, CNV CV, STR, ESP, OSM, ESM, PSV

WHERE ( SMM.SMM_SFAT <> 'C' ) AND

( OSM.OSM_SERIE = SMM.SMM_OSM_SERIE ) AND

( OSM.OSM_NUM = SMM.SMM_OSM ) AND

( ( CV.CNV_COD = NVL ( SMM.SMM_CNV_COD, OSM.OSM_CNV ) ) ) AND

( STR.STR_COD = SMM.SMM_STR ) AND

( SMK.SMK_TIPO = SMM.SMM_TPCOD ) AND

( SMK.SMK_COD = SMM.SMM_COD ) AND

( SMK.SMK_TIPO = CTF.CTF_TIPO ) AND

( SMK.SMK_CTF = CTF.CTF_COD ) AND

( OSM.OSM_PAC = PAC.PAC_REG ) AND

( PSV.PSV_COD = SMM.SMM_MED ) AND

( ESM.ESM_MED = PSV.PSV_COD ) AND

( ESP.ESP_COD = ESM.ESM_ESP ) AND

( ESM.ESM_DEFAULT = 'S' ) AND

( CV.CNV_COD not in ( 'ICB','44' ) ) AND

( STR.STR_NOME not in ( 'ICB','CEC' ) ) AND

( CTF.CTF_CATEG in ( 'C','E' ) ) AND

( ESP.ESP_COD <> 'ICB' ) AND

( SMM.SMM_VLR > 0 ) AND

( SMM.SMM_COD not in ( '1AUX','2AUX','3AUX' ) ) AND

( ( OSM.OSM_DTHR >= to_date ( '01/10/2016 00:00', 'dd/mm/yyyy hh24:mi' ) AND

OSM.OSM_DTHR < to_date ( '27/10/2016 00:00', 'dd/mm/yyyy hh24:mi' ) ) )

group by PAC.PAC_NOME,

SMK.SMK_NOME

having count(distinct CTF.CTF_CATEG) = 2-- distintos = 2 => fez ambos

ORDER BY PAC.PAC_NOME ASC

Compartilhar este post


Link para o post
Compartilhar em outros sites

assim

SELECT PAC.PAC_NOME,
       SMK.SMK_NOME,
       SUM ( SMM.SMM_QT )
 FROM PAC, SMM, SMK, CTF, CNV CV, STR, ESP, OSM, ESM, PSV
 WHERE (  SMM.SMM_SFAT <> 'C'  ) AND
       ( OSM.OSM_SERIE = SMM.SMM_OSM_SERIE ) AND
       ( OSM.OSM_NUM = SMM.SMM_OSM ) AND
       (  ( CV.CNV_COD = NVL ( SMM.SMM_CNV_COD, OSM.OSM_CNV ) )  ) AND
       ( STR.STR_COD = SMM.SMM_STR ) AND
       ( SMK.SMK_TIPO = SMM.SMM_TPCOD ) AND
       ( SMK.SMK_COD = SMM.SMM_COD ) AND
       ( SMK.SMK_TIPO = CTF.CTF_TIPO ) AND
       ( SMK.SMK_CTF = CTF.CTF_COD ) AND
       ( OSM.OSM_PAC = PAC.PAC_REG ) AND
       ( PSV.PSV_COD = SMM.SMM_MED ) AND
       (  ESM.ESM_MED = PSV.PSV_COD ) AND
       (  ESP.ESP_COD = ESM.ESM_ESP ) AND
       (  ESM.ESM_DEFAULT = 'S'  )  AND
       ( CV.CNV_COD not in  ( 'ICB','44' )  ) AND
       ( STR.STR_NOME not in  ( 'ICB','CEC' )  ) AND
( CTF.CTF_CATEG in  ( 'C','E' )  ) AND
       ( ESP.ESP_COD <> 'ICB' ) AND
       ( SMM.SMM_VLR > 0 ) AND
       ( SMM.SMM_COD not in  ( '1AUX','2AUX','3AUX' )  ) AND
       ( ( OSM.OSM_DTHR >= to_date ( '01/10/2016 00:00', 'dd/mm/yyyy hh24:mi' ) AND
           OSM.OSM_DTHR <  to_date ( '27/10/2016 00:00', 'dd/mm/yyyy hh24:mi' ) )    )
 group by PAC.PAC_NOME,
          SMK.SMK_NOME
 ORDER BY PAC.PAC_NOME ASC

retorna o que ?

algum na condição requerida ?

Compartilhar este post


Link para o post
Compartilhar em outros sites

sem querer atrapalhar, tenta assim:

SELECT PAC.PAC_NOME,
SMK.SMK_NOME,
SUM ( SMM.SMM_QT )
sum(decode(CTF.CTF_CATEG,'C',1,0)) Id_Existe_C,
sum(decode(CTF.CTF_CATEG,'E',1,0)) Id_Existe_E
--
FROM PAC, SMM, SMK, CTF, CNV CV, STR, ESP, OSM, ESM, PSV
WHERE ( SMM.SMM_SFAT <> 'C' ) AND
( OSM.OSM_SERIE = SMM.SMM_OSM_SERIE ) AND
( OSM.OSM_NUM = SMM.SMM_OSM ) AND
( ( CV.CNV_COD = NVL ( SMM.SMM_CNV_COD, OSM.OSM_CNV ) ) ) AND
( STR.STR_COD = SMM.SMM_STR ) AND
( SMK.SMK_TIPO = SMM.SMM_TPCOD ) AND
( SMK.SMK_COD = SMM.SMM_COD ) AND
( SMK.SMK_TIPO = CTF.CTF_TIPO ) AND
( SMK.SMK_CTF = CTF.CTF_COD ) AND
( OSM.OSM_PAC = PAC.PAC_REG ) AND
( PSV.PSV_COD = SMM.SMM_MED ) AND
( ESM.ESM_MED = PSV.PSV_COD ) AND
( ESP.ESP_COD = ESM.ESM_ESP ) AND
( ESM.ESM_DEFAULT = 'S' ) AND
( CV.CNV_COD not in ( 'ICB','44' ) ) AND
( STR.STR_NOME not in ( 'ICB','CEC' ) ) AND
( CTF.CTF_CATEG in ( 'C','E' ) ) AND
( ESP.ESP_COD <> 'ICB' ) AND
( SMM.SMM_VLR > 0 ) AND
( SMM.SMM_COD not in ( '1AUX','2AUX','3AUX' ) ) AND
( ( OSM.OSM_DTHR >= to_date ( '01/10/2016 00:00', 'dd/mm/yyyy hh24:mi' ) AND
OSM.OSM_DTHR < to_date ( '27/10/2016 00:00', 'dd/mm/yyyy hh24:mi' ) ) )
group by PAC.PAC_NOME,
SMK.SMK_NOME
having (sum(decode(CTF.CTF_CATEG,'C',1,0)) > 0 and sum(decode(CTF.CTF_CATEG,'E',1,0)) > 0)
ORDER BY PAC.PAC_NOME ASC

Compartilhar este post


Link para o post
Compartilhar em outros sites

Faltou uma virgula

SELECT PAC.PAC_NOME,
SMK.SMK_NOME,
SUM ( SMM.SMM_QT ),
sum(decode(CTF.CTF_CATEG,'C',1,0)) Id_Existe_C,
sum(decode(CTF.CTF_CATEG,'E',1,0)) Id_Existe_E
--
FROM PAC, SMM, SMK, CTF, CNV CV, STR, ESP, OSM, ESM, PSV
WHERE ( SMM.SMM_SFAT <> 'C' ) AND
( OSM.OSM_SERIE = SMM.SMM_OSM_SERIE ) AND
( OSM.OSM_NUM = SMM.SMM_OSM ) AND
( ( CV.CNV_COD = NVL ( SMM.SMM_CNV_COD, OSM.OSM_CNV ) ) ) AND
( STR.STR_COD = SMM.SMM_STR ) AND
( SMK.SMK_TIPO = SMM.SMM_TPCOD ) AND
( SMK.SMK_COD = SMM.SMM_COD ) AND
( SMK.SMK_TIPO = CTF.CTF_TIPO ) AND
( SMK.SMK_CTF = CTF.CTF_COD ) AND
( OSM.OSM_PAC = PAC.PAC_REG ) AND
( PSV.PSV_COD = SMM.SMM_MED ) AND
( ESM.ESM_MED = PSV.PSV_COD ) AND
( ESP.ESP_COD = ESM.ESM_ESP ) AND
( ESM.ESM_DEFAULT = 'S' ) AND
( CV.CNV_COD not in ( 'ICB','44' ) ) AND
( STR.STR_NOME not in ( 'ICB','CEC' ) ) AND
( CTF.CTF_CATEG in ( 'C','E' ) ) AND
( ESP.ESP_COD <> 'ICB' ) AND
( SMM.SMM_VLR > 0 ) AND
( SMM.SMM_COD not in ( '1AUX','2AUX','3AUX' ) ) AND
( ( OSM.OSM_DTHR >= to_date ( '01/10/2016 00:00', 'dd/mm/yyyy hh24:mi' ) AND
OSM.OSM_DTHR < to_date ( '27/10/2016 00:00', 'dd/mm/yyyy hh24:mi' ) ) )
group by PAC.PAC_NOME,
SMK.SMK_NOME
having (sum(decode(CTF.CTF_CATEG,'C',1,0)) > 0 and sum(decode(CTF.CTF_CATEG,'E',1,0)) > 0)
ORDER BY PAC.PAC_NOME ASC

Compartilhar este post


Link para o post
Compartilhar em outros sites

SUM(CASE WHEN CTF.CTF_CATEG = 'C' THEN 1 ELSE 0 END) COND_C ,

...

A sql do post #2 parece correta

Compartilhar este post


Link para o post
Compartilhar em outros sites

Tem uma resposta do Motta sobre o case:

SELECT (CASE WHEN DATA_TYPE = 'CHAR'
THEN (CASE WHEN DATA_LENGTH > 7 THEN 1 ELSE 0 END)
ELSE 0 END)
FROM USER_TAB_COLUMNS

mas em relação ao erro, tire o --, parece que ficou no início da linha do FROM.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.