Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join sub-queries in PL/SQL?

i need to join subqueries in oracle. This does not work i get a syntax error for the join operation

select s1.key, s1.value, s2.value 
from ((select key, value
        from tbl 
        where id = 1) as s1
join 
    (select key, value
        from tbl 
        where id = 2) as s2
on s1.contract = s2.contract);
like image 716
mrt181 Avatar asked Dec 05 '22 00:12

mrt181


2 Answers

You should select the field you are joining on (contract) in the inline views:

SELECT  s1.key, s1.value, s2.value 
FROM    (
        SELECT contract, key, value
        FROM   tbl 
        WHERE  id = 1
        ) as s1
JOIN    (
        SELECT  contract, key, value
        FROM    tbl 
        WHERE   id = 2
        ) as s2
ON     s1.contract = s2.contract
like image 128
Quassnoi Avatar answered Dec 08 '22 15:12

Quassnoi


You had one too many sets of Parenthesis.

SELECT
  s1.key, 
  s1.value, 
  s2.value 
FROM (SELECT 
        key, 
        value
      FROM tbl 
      WHERE id = 1) AS s1
JOIN (SELECT 
        key, 
        value
      FROM tbl 
      WHERE id = 2) AS s2
  ON s1.contract = s2.contract;
like image 29
John Hartsock Avatar answered Dec 08 '22 15:12

John Hartsock