I got a problem in my query, that I think that can be resolved with a subselect.
I have a table called San_Proposta that has a primary key called Proposta_Id. In this table (San_Proposta) I have some columns, but a specific column is called StatusProposta_Id. StatusProposta_Id can only has 2 values: 1 or 2. If SanProposta_Id is 1, then the Proposta_Id don’t exists in a table called San_Negocio that has a foreign key called Proposta_Id. If StatusProposta_Id is 2, then the Proposta_Id exists in my table called San_Negocio.
In San_Negocio I have some columns, but let pay attention in 2 columns in specific: ValidaVenda and ValidaCaptacao. Both columns can only have 2 values: 1 or 0.
I want to do only one query that
- returns all data that don’t exists in San_Negocio, (and how a told early, if San_Proposta.Proposta_Id is 1, then don’t exist in San_Negocio),
- returns all data that exists in San_Proposta but not exists in San_Negocio (and how a told early, if San_Proposta.Proposta_Id is 2, then exists in San_Negocio)
- If the San_Proposta.Proposta_Id exists on my table San_Negocio, (to Proposta_Id exists in my San_Negocio table, the San_Proposta.StatusProposta_Id has the value 2), my column called San_Negocio.ValidaVenda can’t has the same value of San_Negocio.ValidaCaptacao.
How can I do that ?
I tryied the follow query, but don’t work correctly
select
San_Proposta.Proposta_Id
from
San_Proposta
left outer join
(
select
*
from
San_Negocio
where
Proposta_Id is not null
and ValidaCaptacao <> ValidaVenda
) AS Negocio2
on San_Proposta.Proposta_Id = Negocio2.Proposta_Id
where
San_Proposta.StatusProposta_Id IN (1,2)
@Updated
San_Proposta
Proposta_Id | StatusProposta_Id
1 1
2 1
3 1
4 2
5 2
6 2
San_Negocio
Proposta_Id | ValidaVenda | ValidaCaptacao
4 1 0
5 0 1
6 1 1
@Updated
What I Expect
Proposta_Id
1
2
3
4
5
I want that the query returns Proposta_Id 1,2,3 (because don’t exist in San_Negocio) and Proposta_Id 4, 5 because they exist in San_Negocio but ValidaVenda is different then ValidaCaptacao.
1 Answer