0

I am looking for a reliable algorithmic way to nest queries in Oracle without having to parse and rebuild SQL statements. In DB2, MYSQL, and SQLITE I can simply nest from the largest columns set in the inner subquery to the smallest column set in the very outer query like this:

  SELECT A, B FROM (
       SELECT A, B, C FROM (
            SELECT A, B, C, D FROM (
                 SELECT * FROM myTable
                 )
            )
      )
 )

This does not work for Oracle, I had to use the WITH clause but that allows me one level of nesting like

 WITH Q1 as (
      SELECT * FROM myTable 
            ) 
            SELECT A, B FROM Q1

is there an algorithmic way to nest select statements in Oracle like DB2 or MySQL above

You can always assume it is a single table. The queries are generated automatically

Found the root cause:

My code assumes an "AS alias" is needed and appends it to the subquery as in "Select A, B from (select A, B, C from myTable) AS Q1" --- Oracle does not like that and gives an error "missing right parenthesis"

1
  • 1
    You have one-too-many right parentheses in the code you posted (four rights but only three lefts). Share and enjoy. Commented Jul 15, 2013 at 2:03

1 Answer 1

1

What do you mean by "does not work"? The structure you seem to want works perfectly well in Oracle

SQL> select ename from (
  2    select ename, empno from (
  3      select ename, empno, job from (
  4        select ename, empno, job, mgr from (
  5          select * from emp
  6        )
  7      )
  8    )
  9  );

ENAME
----------
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER

14 rows selected.
Sign up to request clarification or add additional context in comments.

1 Comment

If I was wrong (and I hope I am) then my code failed on the Oracle wrapper only and I am investigating why. Thank you in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.