Subselect in Oracle database

3

I am trying to compile a query and within one of my joins it is necessary to make a subselect to ensure the integrity of the query as the example below:

select t01.teste           
from teste t01                
left join tes_teste_2 t02                   
on t01.isn_teste = t02.isn_teste           
and t02.isn_pessoa = (select min(t04.isn_pessoa) from tes_teste t04 
where t04.isn_teste = t01.isn_teste)
where t01.isn_empresa = 666

The problem that returns me that " ORA-01799: a column can not be externally attached to a subquery "

I would like some help how I could compile this subselect as a condition. Emphasizing that we use Oracle 11G.

    
asked by anonymous 17.11.2015 / 21:22

2 answers

0

An alternative would be

select t01.teste           
from teste t01
inner join 
(
   select isn_teste, min(isn_pessoa) isn_pessoa
   from   tes_teste 
   group by isn_teste
) t04         
  on t04.isn_teste = t01.isn_teste      
left join tes_teste_2 t02                   
  on t01.isn_teste = t02.isn_teste           
 and t02.isn_pessoa = t04.isn_pessoa
where t01.isn_empresa = 666

Another alternative would be to pass the condition to the where

select t01.teste           
from teste t01                
inner join tes_teste_2 t02                   
on t01.isn_teste = t02.isn_teste           
where t01.isn_empresa = 666
  and t02.isn_pessoa = (
         select min(t04.isn_pessoa) 
           from tes_teste t04 
          where t04.isn_teste = t01.isn_teste)
    
17.11.2015 / 22:33
0

A subquery can return more than one record, so you should not use the = operator instead of in .

A different way to do this query is:

select a.teste
  from teste a,
       tes_teste_2 b
 where a.isn_teste = b.isn_teste
   and b.isn_pessoa in ( select min(c.isn_pessoa)
                           from tes_teste c
                          where c.isn_teste = a.isn_teste )
   and a.isn_empresa = 666;
    
17.11.2015 / 23:43