Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
lailarhc
Creator
Creator

Check if value exists in one table before selecting null value in another table

Hello.
I'm trying to combine two tables with Union. I managed to do that, but I need to check if one value exists in table 1 (V_AuditoriaEventoServidor) before selecting the corresponding null value in table 2 (SHC_CapacitacaoNaoInstitucional). If value does not exists in table 1, then I want to select the null value in table 2. If value does exists in table 1, then I should not select null value in table 2.

I'm combining the two tables using UNION.

Where should I insert my IF? Should I declare a variable and set it to TRUE if value exists? How can I go on about doing this?

Any help is appreciated.
Thank you in advance.

Here's my script:

//If Empregado.Curso exists in this table then null value from second table should not count
SQL SELECT NomeEmpregado as Servidor,
  evento."DataIniRealizacao" as Início,
  evento."DataFimRealizacao" as Fim,
  evento."CargaHoraria" as Horas,
  evento."descEvento" as Curso
FROM SARH.dbo.Empregado
LEFT JOIN SDTA.dbo."V_AuditoriaEventoServidor" as evento
ON evento.numCpf = Empregado.NumCpf
LEFT JOIN SARH.dbo.DadosFuncionais as dados
ON dados.CodEmpregado = Empregado.CodEmpregado
JOIN SARH.dbo.Orgao as orgao
ON orgao.CodOrgao = dados.CodUltimaLotacao
AND orgao.SiglaOrgao LIKE 'GR08%'
WHERE dados.DataSaidaEmpregado is null

UNION

SELECT NomeEmpregado as Servidor2,
  capacitacao."DataInicio" as Início2,
  capacitacao."DataFim" as Fim2,
  capacitacao."CargaHoraria" as Horas2,
  capacitacao."descTitulo" as Curso2
FROM SARH.dbo.Empregado
LEFT JOIN SDTA.dbo."SHC_CapacitacaoNaoInstitucional" as capacitacao
ON capacitacao.CodEmpregado = Empregado.CodEmpregado
LEFT JOIN SDTA.dbo."V_AuditoriaEventoServidor" as evento2
ON evento2.numCpf = Empregado.NumCpf
LEFT JOIN SARH.dbo.DadosFuncionais as dados
ON dados.CodEmpregado = Empregado.CodEmpregado
JOIN SARH.dbo.Orgao as orgao
ON orgao.CodOrgao = dados.CodUltimaLotacao
AND orgao.SiglaOrgao LIKE 'GR08%'
WHERE dados.DataSaidaEmpregado is null;

Labels (3)
2 Replies
Vikash
Contributor III
Contributor III

Hi,

Try function exists() function in where clause.

VK

lailarhc
Creator
Creator
Author

Hi @Vikash . Could you show me how, exactly?