How to join subqueries in PL / SQL?

I need to join the 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);
+3
source share
3 answers

You must select the field you enter ( contract) in the built-in 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
+9
source

You had too many sets of brackets.

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;
+2
source

Get rid of the outer bracket.

+1
source

Source: https://habr.com/ru/post/1781678/


All Articles