Left Join returning only 1 record for each relationship

1

I need to do a Join between 2 tables where I need only to display the last record of the 2nd table (only 1 record of table 2 for each record of table 1).

SELECT a.Campo1, a.Campo2, a.Campo3, a.Campo4, b.Campo1, b.Campo2
FROM Tabela1 a
LEFT JOIN Tabela2 b ON b.Campo1 = a.Campo1 AND  (b.Campo2 = (SELECT b.Campo2 FROM Tabela2 c  WHERE c.Campo1 = a.Campo1) AND ROWNUM = 1)

trying this way returns the error:

  

ORA-01799: A column can not be externally bound to a subquery

Can anyone help me?

    
asked by anonymous 20.02.2018 / 21:21

1 answer

0

See if this works:

SELECT a."Campo1"
     , a."Campo2"
     , a."Campo3"
     , a."Campo4"
     , b."Campo1"
     , b."Campo2"
  FROM Tabela1 a
  LEFT JOIN (SELECT MAX(Tabela2."Campo2") AS "Campo2"
                  , Tabela2."Campo1" 
               FROM Tabela2 
              GROUP BY Tabela2."Campo1") b ON b."Campo1" = a."Campo1"
    
20.02.2018 / 21:46