Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Consulta SQL para retornar dados somente se TODAS as colunas necessárias estiverem presentes e não NULL


Você pode usar exists . Acho que você pretende:
select t.*
from t
where exists (select 1
              from t t2
              where t2.id = t.id and t2.type = 'Purchase' and t2.total is not null
             ) and
      exists (select 1
              from t t2
              where t2.id = t.id and t2.type = 'Exchange' and t2.total is not null
             ) and
      exists (select 1
              from t t2
              where t2.id = t.id and t2.type = 'Return' and t2.total is not null
             );

Existem maneiras de "simplificar" isso:
select t.*
from t
where 3 = (select count(distinct t2.type)
           from t t2
           where t2.id = t.id and
                 t2.type in ('Purchase', 'Exchange', 'Return') and
                 t2.total is not null
          );