1
I have two tables called Protocol and Analyze with a 1:n relationship, a Protocol may have several Analysis, Protocol has a composite primary key. I need to write an SQL that returns the Protocols that have no Analysis and the Protocols that have at least one Analysis. I tried to write some, but without success, the queries do not return the expected items, or return nothing
|PROTOCOLO | |ANALISE |
|prefixo PK | |idanalise PK |
|numero PK | |prefixo FK |
|ano PK | |numero FK |
|// OUTROS ATTRS| |ano FK |
|// OUTROS ATRIBUTOS |
SQL I tried to do for Protocols without any analysis
SELECT * FROM protocolo p WHERE p.prefixo
NOT IN (SELECT a.protocolo_prefixo
FROM analise a)
AND p.numero NOT IN (SELECT a.protocolo_numero
FROM analise a)
AND p.ano NOT IN (SELECT a.protocolo_ano
FROM analise a)
SQL I tried to do for protocols with at least 1 parsing
SELECT * FROM protocolo p WHERE p.prefixo
IN (SELECT a.protocolo_prefixo
FROM analise a)
AND p.numero IN (SELECT a.protocolo_numero
FROM analise a)
AND p.ano IN (SELECT a.protocolo_ano
FROM analise a)
I made an online example to use base to solve the problem: http://sqlfiddle.com/#! 17/bbabe/13
– David
@David, thanks for your help.
– Juliana Marques